Solution for Exercise PrintNumberInWord (nested-if, switch-case) are given below.
Using If Else The solution goes like this
Using Switch Case The code goes like this.
Using If Else The solution goes like this
public class PrintNumberInWord { // saved as "PrintNumberInWord.java" public static void main(String[] args) { int number = 5; // Using nested-if if (number == 1) { System.out.println("ONE"); } else if (number == 2) { System.out.println("TWO"); } else if (number == 3) { System.out.println("THREE"); } else if (number == 4) { System.out.println("FOUR"); } else if (number == 5) { System.out.println("FIVE"); } else if (number == 6) { System.out.println("SIX"); } else if (number == 7) { System.out.println("SEVEN"); } else { System.out.println("SEVEN"); } } }
Using Switch Case The code goes like this.
public class PrintNumberInWord { // saved as "PrintNumberInWord.java" public static void main(String[] args) { int number = 5; // Using switch-case switch(number) { 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; default : System.out.println("OTHER"); } } }
No Comment to " Solution For Exercise PrintNumberInWord "