18. Write a program to input a digit between 0 to 9 using Scanner class. Using switch-case print
the number name. E.g., if input is 7; then, the output is Seven otherwise print a message "Out
of range", if the value of digit is other than 0-9.
Answers
The given problem is solved using language - Java.
import java.util.Scanner;
public class Brainly{
public static void main(String s[]){
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number (0-9): ");
switch(sc.nextInt()){
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("Out of range. ");
}
sc.close();
}
}
- Accept the number from the user.
- Use switch case statement to print the number name.
- If the number is out of range, display an appropriate message using default keyword.
See attachment for output.
Answer:
import java.util.*;
class NumberNames
{
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
int n=sc.nextInt();
switch(n)
{
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("Out of range");
break;
}
}
}
}