INTERPRET THE PROGRAM AND WRITE THE OUTPUT, THEN WRITE THE PROGRAM AND EXECUTE TO VERIFY IT. IF YOUR ANSWER WAS WRONG THEN UNDERSTAND WHY IT IS AND WRITE A BRIEF NOTE ABOUT HOW PROGRAM IS WORKING.

ANSWER :

PROGRAM :  1

In the below program if expression returns false ; so else block will execute.And output :

Radius should be greater than zero

class AreaWithIfElse{
    public static void main(String[] felight){
        final double PI=3.142;
        double radius = -123.123;
        //if the output of the expression is true
        //the the body of if will execute
        if(radius>0){ //condition false
            double area = radius*radius*PI;
            System.out.println(area);
        } else
            System.out.println("Radius should be greater than zero");
        //else block will execute
    }
}

/*
D:\Felight\Basic Java>javac AreaWithIfElse.java

D:\Felight\Basic Java>java AreaWithIfElse
Radius should be greater than zero
*/

 

PROGRAM :  2

In the below program condition if is false ;so the else block will be execute.So output :

11 is odd

//if-then-else statement
//program check of a number if its even or odd
class EvenOrOdd{
    public static void main(String[] felight){
        int num=11;
        if(num%2==0)//condition false
            System.out.println(num + " is Even");
        else //else block will execute
            System.out.println(num + " is Odd");
    }
}

/*

D:\Felight\Basic Java>javac EvenOrOdd.java

D:\Felight\Basic Java>java EvenOrOdd
11 is Odd

*/

 

PROGRAM :  3

In the below program condition if is false ; so the else block will be execute.So the output should be :

num2 is greater

//if-then-else statement
/*Program checks largest of two */
class LargestOfTwo{
    public static void main(String[] felight){
        int num1=10;
        int num2=20;
        if(num1>num2)//condition false
            System.out.println("num1 is greater ");
        else //else block will execute
            System.out.println("num2 is greater");
    }
}

/*
D:\Felight\Basic Java>javac LargestOfTwo.java

D:\Felight\Basic Java>java LargestOfTwo
num2 is greater
*/

 

PROGRAM :  4

In the below program both the if condition is true.So the output should be:

Num1 is greater than num2 & num3

//Nested if example
class NestedIf{
    public static void main(String[] felight){
        int num1=100;
        int num2=20;
        int num3=60;
        if(num1>num2) //true
            if(num1>num3) //true
                System.out.println("Num1 is greater than num2 & num3");
    }
}

/*

D:\Felight\Basic Java>javac NestedIf.java

D:\Felight\Basic Java>java NestedIf
Num1 is greater than num2 & num3

*/

 

PROGRAM :  5

In the below program all three if expression is true.So the must be :

Num is between 0 and 10 and even

//nested if
class NestedIfExample2{
    public static void main(String[] felight){
        int num=6;
        if(num>0) //true
            if(num<10) //true
                if(num%2==0) //true
                    System.out.println("Num is between 0 and 10 and even");
    }
}

/*

D:\Felight\Basic Java>javac NestedIfExample2.java

D:\Felight\Basic Java>java NestedIfExample2
Num is between 0 and 10 and even

*/

 

PROGRAM :  6

In the below program  first two if expression is false;so the the else block will execute.So the output must be:

-10 is -ve number

 

//Simple example for if-then-else-if statement
/* Program to check if a number is +ve or -ve oe 0*/
class PositiveOrNegativeOrZero{
    public static void main(String[] felight){
        int number = -10;
        if (number==0) { //false
            System.out.println(number + " is Zero");
        }else if(number>0){ //false
            System.out.println(number + " is +ve number");
        }else{ //true
            System.out.println(number + " is -ve number");
        }
    }
}

/*

D:\Felight\Basic Java>javac PositiveOrNegativeOrZero.java

D:\Felight\Basic Java>java PositiveOrNegativeOrZero
-10 is -ve number

*/
PROGRAM :  7

In the below program  first if expression is false and 2nd one true; so the output must be:

Your Grade is B

//Simple example for if-then-else-if statement
/* Program to allocate the grade according to percentage*/
class Grade{
    public static void main(String[] felight){
        int score=60;
        char grade='X';

        if (score>=75) { //false
            grade='A';
        }else if(score>=60){ //true
            grade='B';
        }else if(score>=50){
            grade='C';
        }else if(score>=35){
            grade='D';
        }else {
            grade='F';
        }
        System.out.println("Your Grade is " + grade);
    }
}

/*

D:\Felight\Basic Java>javac Grade.java

D:\Felight\Basic Java>java Grade
Your Grade is B

*/