Respuesta :
Answer:
The JAVA program is shown below.
import java.util.Scanner;
import java.lang.*;
public class Roman {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int num;
do
{
System.out.println("Enter any number from 1 to 10 to display it in Roman Number");
num = sc.nextInt();
if(num<1 || num>10)
{
System.out.println("Invalid input.");
System.out.println("Enter any number from 1 to 10 to display it in Roman Number");
num = sc.nextInt();
}
}while(num<1 || num>10);
System.out.print("The Roman numeral for "+num+" is ");
switch(num)
{
case 1: System.out.println("I");
break;
case 2: System.out.println("II");
break;
case 3: System.out.println("III");
break;
case 4: System.out.println("IV");
break;
case 5: System.out.println("V");
break;
case 6: System.out.println("VI");
break;
case 7: System.out.println("VII");
break;
case 8: System.out.println("VIII");
break;
case 9: System.out.println("IX");
break;
case 10: System.out.println("X");
break;
}
}
}
OUTPUT
Enter any number from 1 to 10 to display it in Roman Number
100
Invalid input.
Enter any number from 1 to 10 to display it in Roman Number
11
Enter any number from 1 to 10 to display it in Roman Number
7
The Roman numeral for 7 is VII
Explanation:
This program begins where user inputs a number from 1 to 10 to display its equivalent roman numeral.
The user input is validated using if statement inside a do while loop.
do
{
System.out.println("Enter any number from 1 to 10 to display it in Roman Number");
num = sc.nextInt();
if(num<1 || num>10)
{
System.out.println("Invalid input.");
System.out.println("Enter any number from 1 to 10 to display it in Roman Number");
num = sc.nextInt();
}
}while(num<1 || num>10);
The switch case is executed over the user input. Each switch case ends with a break statement. When a particular case from the switch statement is satisfied and executed, the switch statement is executed by break.
There is no default case in switch statement since user input is validated before entering the switch statement.