Close
Close

Practice questions on Input by user

Level 1

1.
Write a program to take two integer inputs from user and print sum and product of them.
import java.util.Scanner;
class Ans{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    int x = s.nextInt();
    int y = s.nextInt();
    System.out.println(x+y);
  }
}

2.
Take two integer inputs from user. First calculate the sum of two then product of two. Finally, print the sum and product of both obtained results.
import java.util.Scanner;
class Ans{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    int x = s.nextInt();
    int y = s.nextInt();
    int z = x+y;
    int p = x*y;
    System.out.println("Sum : "+z+"\nProduct : "+p);
  }
}

3.
Ask user to give two double input for length and breadth of a rectangle and print area type casted to int.
import java.util.Scanner;
class Ans{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    double x = s.nextDouble();
    double y = s.nextDouble();
    double area = x*y;
    System.out.println((int)area);
  }
}

4.
Take name, roll number and field of interest from user and print in the format below :
Hey, my name is xyz and my roll number is xyz. My field of interest are xyz.
import java.util.Scanner;
class Ans{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter name");
    String a = s.next();
    System.out.println("Enter roll number");
    int b = s.nextInt();
    System.out.println("Enter Filed of interest");
    s.next();//To consume '\n' left by ENTER KEY, it is not consumed by nextInt()
    String c = s.nextLine();
    System.out.println("Hey, my name is "+a+" and my roll number is "+b+". My field of interest are "+c+".");
  }
}

5.
Take side of a square from user and print area and perimeter of it.
import java.util.Scanner;
class Ans{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter side");
    int a = s.nextInt();
    System.out.println("Area : "+(a*a)+"\nPerimeter : "+(4*a));
  }
}

6.
Write a program to find square of a number.
E.g.-
INPUT : 2        OUTPUT : 4
INPUT : 5        OUTPUT : 25

7.
Take two different string input and print them in same line. E.g.-
INPUT : Codes
Dope
OUTPUT : CodesDope

8.
Take 3 inputs from user and check :
all are equal
any of two are equal
( use && || )
import java.util.Scanner;
class Ans{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter first number");
    int a = s.nextInt();
    System.out.println("Enter second number");
    int b = s.nextInt();
    System.out.println("Enter third number");
    int c = s.nextInt();
    System.out.println("All are equal : "+(a==b&&b==c&&c==a));
    System.out.println("Any two are equal : "+(a==b||b==c||c==a));
  }
}

9.
Write a program to enter the values of two variables 'a' and 'b' from keyboard and then check if both the conditions 'a < 50' and 'a < b' are true.

10.
If the marks of Robert in three subjects are entered through keyboard (each out of 100 ), write a program to calculate his total marks and percentage marks.

Level 2

Level 3

Ask Yours
Post Yours