27-12-24 ----------- Q)When to use "if else" statement. =>Whenever two mutually exclusive conditions are to be tested we should go for "if else" statement. For eg. finding if a number is even or odd Q)Java application to find if a number is even or odd. //evenodd.java import java.util.Scanner; class EvenOdd{ public static void main(String a[]){ Scanner scanner=new Scanner(System.in); System.out.print("Enter the number:"); int n=scanner.nextInt(); //8 if(n%2==0){ System.out.println("It is an even number"); } else{ System.out.println("It is an odd number"); } } } Q)When to use if else ladder statement? =>Whenever more than 2 mutually exclusive conditions are to be verified, we go for "if else ladder". Syntax -------- if(condition) { //statements } else if(condition) { //statements } else if(condition) { //perfrom the task } else { //task perfroming code } Q)Java application to find if a number is +ve or -ve. //positivenegative.java import java.util.Scanner; class PositveNegative{ public static void main(String args[]){ Scanner scanner=new Scanner(System.in); System.out.print("Enter the number:");//0 int n=scanner.nextInt(); if(n>0){ System.out.println("It is +ve"); } else if(n<0){ System.out.println("It is -ve"); } else{ System.out.println("It is ZERO"); } } } Q)When to use nested if statement in a Java application? =>Writing an if statement within the body of another if statement is nothing but a nested if. if(condition){ //outer if if(cindition){ //nested if OR inner if } } =>Whenever more than one level of condition checking is the requirement, we go for nested if statement. Q)Java application to find if a number is +ve even,+ve odd,-ve even OR -ve odd. //positiveevenodd.java import java.util.Scanner; class PositveEvenOdd{ 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 +ve even"); } else{ System.out.println("It is +ve odd"); } } else { if(n%2==0){ System.out.println("It is -ve even"); } else{ System.out.println("It is -ve odd"); } } } } Q)Develop a Java application to display a digit 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(0-9):"); 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 digit only"); } } } Note:- There are two multi way branching statements in Java. 1)if else ladder statement 2)switch statement //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(0-9):"); int digit=scanner.nextInt(); switch(digit){ case 0: System.out.println("ZERO"); break; case 1: System.out.println("ONE"); break; case 2: System.out.println("TWO"); break; case 3: System.out.println("THREE"); break; case 4: System.out.println("FOUR"); break; case 5: System.out.println("FIVE"); break; case 6: System.out.println("SIX"); break; case 7: System.out.println("SEVEN"); break; case 8: System.out.println("EIGHT"); break; case 9: System.out.println("NINE"); break; default: System.out.println("Please enter digit only"); }//switch } }