09/01/2025 -------------- Q)Modifiy the previous Java application so as to use "switch statement" in place of "if else ladder" statement. //SwitchDigit.java import java.util.Scanner; class SwitchDigit{ public static void main(String[] args) { Scanner scanner=new Scanner(System.in); System.out.print("Enter a digit:"); 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("Invalid input.Please enter between 0 and 9 only"); }//switch } //main }//class Q)Explain about for statement. =>whenever any code is required to be executed repeatedly, we go for "for statement/loop". Syntax -------- for(loop counter initialization;condition; loop counter updation){ //statements } =>for statement has 4 parts. 1)loop counter initialization 2)condition 3)body 4)loop counter incrementaion/decrementation Note:- a variable used in the condition is known as a loop counter. working of for loop -------------------- =>Loop counter is initialized first.It happens only once in the life time of the for loop. =>condition is verified. when the condition is false, loop is exited(control comes out of the lopp).when the condition is true, body of the loop is executed. =>After executing the body, loop counter is either incremented or decremented(updated). =>Again condition is verified. when the condition is false, loop is exited(control comes out of the lopp).when the condition is true, body of the loop is executed. It goes on. Q)Java program to display first 10 natural numbers using for loop. class Test{ public static void main(String[] args){ for(int i=1;i<=10;i++){ System.out.println(i); } } } Q)Java program to display first 10 natural numbers using while loop. class Test{ public static void main(String[] args){ int i=1;//loop counter initialization while(i<=10){ System.out.println(i); i++; } } } Q)Java program to display first 10 natural numbers using do while loop. class Test{ public static void main(String[] args){ int i=1;//loop counter initialization do{ System.out.println(i); i++; }while(i<=10); } } Q)Java program to display numbers from 10 to 1. class Test{ public static void main(String[] args){ for(int i=10;i>=1;i--){ System.out.println(i); } } } Q)When to use for loop and when to use while loop? =>We can replace any loop with any another type of loop. I.e. we can use any loop. =>However, we better use for loop if we know in advance how many number of times the loop it has to be repeated. =>When we don't know in advance how many repetetions are involved, we go for while loop. Q)Java program to find the sum of the digits of a number. import java.util.Scanner; class Test{ public static void main(String[] args){ Scanner scanner=new Scanner(System.in); System.out.print("Enter the number:");//234 int n=scanner.nextInt(); int sum=0; while(n !=0){ sum=sum+n%10; n=n/10; } System.out.println("The sum is "+sum); } }