08/01/24 ---------- Q)Explain about "simple if statement". =>An if statement without else statement is known as a simple if statement. =>simple if statement has the following syntax. if(condition){ statements } Note:- code written between the flower braces is nothing but the body of the simple if statement. =>When the condition is true, body of the simple if statement is executed. When the condition is false, its body is not executed. Q)What is the output of the following Java application? class Test { public static void main(String[] args) { int a=10,b=20,c=40; if(a>b && bb || bwhenever two mutually exclusive conditions are to be verified for selection, we go for "if else statement". Q)Java program to find if a number is even or odd. //EvenOdd.java import java.util.Scanner; class EvenOdd{ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.print("Enter the number:"); int n=scanner.nextInt(); if(n%2==0){ System.out.println("It is an even number"); } else{ System.out.println("It is an odd number"); } } } Q)When is if else ladder statement used? =>whenever more than 2 mutually exclusive conditions are to be verified, we go for "if else ladder" statement. Q)Java program to find if a number is +ve or -ve. import java.util.Scanner; class PositiveNegative{ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.print("Enter the number:"); int n=scanner.nextInt(); if(n>0){ System.out.println("It is a +ve number"); } else if(n<0){ System.out.println("It is a -ve number"); } else{ System.out.println("It is neither +ve, nor -ve. It is ZERO"); } } } Q)What is a nested if statement? =>An if statement written within the body of another if statement is nothing but a nested if statement. For eg. if(condition){ //outer if OR enclosing if if(condition){ //nested if OR inner if } } Q)When is nested if used? =>Whenever more than one level of condition is required to be verified, we go for nested if statement. Q)Write a Java program to find if a number is +ve even,+ve odd,-ve even or -ve odd program. import java.util.Scanner; class PositiveEvenOdd{ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.print("Enter the number:"); int n=scanner.nextInt(); if(n>0){ if(n%2==0){ System.out.println("It is a +ve even number."); }//nested if else{ System.out.println("It is a +ve odd number."); } }//outer if else if(n<0){ if(n%2==0){ System.out.println("It is a -ve even number."); } else{ System.out.println("It is a -ve odd number."); } }//outer if else{ System.out.println("IT IS ZERO"); } } } Q)Java program to read a digit from the keyboard and display in word. //Digit.java import java.util.Scanner; class Digit{ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.print("Enter a digit:"); int digit=scanner.nextInt(); if(digit==0){ System.out.println("ZERO"); } else if(digit==1){ System.out.println("ONE"); } else if(digit==2){ System.out.println("TWO"); } else if(digit==3){ System.out.println("THREE"); } else if(digit==4){ System.out.println("FOUR"); } else if(digit==5){ System.out.println("FIVE"); } else if(digit==6){ System.out.println("SIX"); } else if(digit==7){ System.out.println("SEVEN"); } else if(digit==8){ System.out.println("EIGHT"); } else if(digit==9){ System.out.println("NINE"); } else{ System.out.println("Invalid input.Please enter between 0 and 9 only"); } } //main }//class Note:- Whenever many mutually excluse conditions are to be verified, better use "switch statement".