Using Java Write a program called PrintNumberInWord which prints \"ONE\", \"TWO\
ID: 3700935 • Letter: U
Question
Using JavaWrite a program called PrintNumberInWord which prints "ONE", "TWO",... , "NINE", "OTHER" if the int variable "number" is 1, 2,... , 9, or other, respectively. Use (a) a "nested-if" statement; (b) a "switch-case" statement.
Using Java
Write a program called PrintNumberInWord which prints "ONE", "TWO",... , "NINE", "OTHER" if the int variable "number" is 1, 2,... , 9, or other, respectively. Use (a) a "nested-if" statement; (b) a "switch-case" statement.
Write a program called PrintNumberInWord which prints "ONE", "TWO",... , "NINE", "OTHER" if the int variable "number" is 1, 2,... , 9, or other, respectively. Use (a) a "nested-if" statement; (b) a "switch-case" statement.
Explanation / Answer
1.> Using nested If statement
import java.util.Scanner;
public class PrintNumberInWord{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the variable");// takes input from the user
int var=sc.nextInt();
if(var==1) //comparing each variable with nested if
System.out.println("ONE");
else if(var==2)
System.out.println("TWO");
else if(var==3)
System.out.println("THREE");
else if(var==4)
System.out.println("FOUR");
else if(var==5)
System.out.println("FIVE");
else if(var==6)
System.out.println("SIX");
else if(var==7)
System.out.println("SEVEN");
else if(var==8)
System.out.println("EIGHT");
else if(var==9)
System.out.println("NINE");
else
System.out.println("OTHER");
}
}
2.> using switch
import java.util.Scanner;
public class PrintNumberInWord{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the variable");
int var=sc.nextInt();
switch(var)
{
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("OTHER");
break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.