74.8k views
0 votes
Write a program that asks the user to enter a number within the range of 1 through 10. Use a switch statement to display the Roman numeral version of that number. Input Validation: Do not accept a number less than 1 or greater than 10.

1 Answer

2 votes

Answer:

// program in C++.

// headers

#include <bits/stdc++.h>

using namespace std;

// main function

int main()

{

// Variable

int num;

cout << "Enter a number between 1 and 10: ";

// read input

cin >> num;

// validate input number

while(num<1||num>10)

{

// if input is wrong then print error message

cout<<"Wrong input!!Enter again:";

// read again

cin>>num;

}

// print output

cout << "The Roman numeral for "<<num<<" is:";

// switch

switch (num)

{

case 1:

// if input is 1

cout<<"I"<<endl;

// exit the switch

break;

case 2:

// if input is 2

cout<<"II"<<endl;

// exit the switch

break;

case 3:

// if input is 3

cout<<"III"<<endl;

// exit the switch

break;

case 4:

// if input is 4

cout<<"IV"<<endl;

// exit the switch

break;

case 5:

// if input is 5

cout<<"V"<<endl;

// exit the switch

break;

case 6:

// if input is 6

cout<<"VI"<<endl;

// exit the switch

break;

case 7:

// if input is 7

cout<<"VII"<<endl;

// exit the switch

break;

case 8:

// if input is 8

cout<<"VIII"<<endl;

// exit the switch

break;

case 9:

// if input is 9

cout<<"IX"<<endl;

// exit the switch

break;

case 10:

// if input is 10

cout<<"X"<<endl;

// exit the switch

break;

// default

default:

break;

}

return 0;

}

Step-by-step explanation:

Read a number from usr and assign it to variable "num". If the input number is less than 1 or greater than 10 then ask again to enter a number until user enter a number between 1-10 only.Then with the help of switch() function print the equivalent Roman number.

Output:

Enter a number between 1 and 10: -5

Wrong input!!Enter again:12

Wrong input!!Enter again:6

The Roman numeral for 6 is:VI

User Ulyana
by
8.0k points