Close
Close

Practice questions on Decide if or else

Level 1

1.
Take values of length and breadth of a rectangle from user and check if it is square or not.
import java.util.Scanner;
class Ans{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter length");
    int x = s.nextInt();
    System.out.println("Enter breadth");
    int y = s.nextInt();
    if(x==y){
      System.out.println("Square");
    }
    else{
      System.out.println("Rectangle");
    }
  }
}

2.
Take two int values from user and print greatest among them.

3.
A shop will give discount of 10% if the cost of purchased quantity is more than 1000.
Ask user for quantity
Suppose, one unit will cost 100.
Judge and print total cost for user.
import java.util.Scanner;
class Ans{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter quantity");
    int x = s.nextInt();
    if((x*100)>1000){
      System.out.println("You get a discount of "+(.1*x*100)+" and your total cost is "+(x*100-(.1*x*100)));
    }
    else{
      System.out.println("No discount");
    }
  }
}

4.
A company decided to give bonus of 5% to employee if his/her year of service is more than 5 years.
Ask user for their salary and year of service and print the net bonus amount.

5.
A school has following rules for grading system:
a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 - A
Ask user to enter marks and print the corresponding grade.
import java.util.Scanner;
class Ans{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter your marks");
    int x = s.nextInt();
    if(x<25){
      System.out.println("F");
    }
    else if((x>=25)&&(x<45)){
      System.out.println("E");
    }
    else if((x>=45)&&(x<50)){
      System.out.println("D");
    }
    else if((x>=50)&&(x<60)){
      System.out.println("C");
    }
    else if((x>=60)&&(x<80)){
      System.out.println("B");
    }
    else if((x>=80)&&(x<=100)){
      System.out.println("A");
    }
    else{
      System.out.println("Not correct marks");
    }
  }
}

6.
Take input of age of 3 people by user and determine oldest and youngest among them.

7.
Write a program to print absolute vlaue of a number entered by user. E.g.-
INPUT: 1        OUTPUT: 1
INPUT: -1        OUTPUT: 1
import java.util.Scanner;
class Ans{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter number");
    int x = s.nextInt();
    if(x<0){
      System.out.println("Absolute value : "+(-1*x));
    }
    else{
      System.out.println("Absolute value : "+x);
    }
  }
}

8.
A student will not be allowed to sit in exam if his/her attendence is less than 75%.
Take following input from user
Number of classes held
Number of classes attended.
And print
percentage of class attended
Is student is allowed to sit in exam or not.

9.
Modify the above question to allow student to sit if he/she has medical cause. Ask user if he/she has medical cause or not ( 'Y' or 'N' ) and print accordingly.

10.
If
x = 2
y = 5
z = 0
then find values of the following expressions:
a. x == 2
b. x != 5
c. x != 5 && y >= 5
d. z != 0 || x == 2
e. !(y < 10)
class Ans{
  public static void main(String[] args){
    int x = 2;
    int y = 5;
    int z = 0;
    System.out.println(x==2);
    System.out.println(x != 5);
    System.out.println(x != 5 && y >= 5);
    System.out.println(z != 0 || x == 2);
    System.out.println(!(y < 10));
  }
}

11.
Write a program to check whether a entered character is lowercase ( a to z ) or uppercase ( A to Z ).
You can use ASCII value to do so.

Level 2

1.
Write a program to check if a year is leap year or not.
If a year is divisible by 4 then it is leap year but if the year is century year like 2000, 1900, 2100 then it must be divisible by 400.

2.
Ask user to enter age, sex ( M or F ), marital status ( Y or N ) and then using following rules print their place of service.
if employee is female, then she will work only in urban areas.

if employee is a male and age is in between 20 to 40 then he may work in anywhere

if employee is male and age is in between 40 t0 60 then he will work in urban areas only.

And any other input of age should print "ERROR".

3.
A 4 digit number is entered through keyboard. Write a program to print a new number with digits reversed as of orignal one. E.g.-
INPUT : 1234        OUTPUT : 4321
INPUT : 5982        OUTPUT : 2895
import java.util.Scanner;
class Ans{
  public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter number");
    int x = s.nextInt();
    int first_digit = x%10;
    int second_digit = (x/10)%10;
    int third_digit = (x/100)%10;
    int fourth_digit = (x/1000)%10;
    int new_number = (first_digit*1000)+(second_digit*100)+(third_digit*10)+(fourth_digit*1);
    System.out.println(new_number);
  }
}

Level 3

Ask Yours
Post Yours