21.4k views
3 votes
Write a program that displays the roman numeral equivalent of any decimal number between 1 and 20 that the user enters. The roman numerals should be stored in an array of strings and the decimal number that the user enters should be used to locate the array element holding the roman numeral equivalent. The program should have a loop that allows the user to continue entering numbers until an end sentinel of 0 is entered.

User Or Neeman
by
6.7k points

1 Answer

2 votes

Here is code in c++.

// include headers

#include <bits/stdc++.h>

using namespace std;

// main function

int main ()

{

// string array initialize with roman number equivalent of 1 to 10

string rmn[21] = { " ", "I", "II", "III", "IV", "V","VI", "VII", "VIII", "IX", "X"};

// declare variable to take input

int x;

// check for a valid input

do

{

do

cout << "Enter a number and between 1 and 20\\"

<< "Enter '0' to exit program: ";

cin >> x;

if (x < 0

while (x < 0 || x > 20);

// print equivalent roman number

if(x<=10)

{

cout << rmn[x] << endl;

}

else

{

x=x-10;

cout<<"X"<< rmn[x] << endl;

}

}

// if input is 0 then end the program

while (x != 0);

cout << endl;

return 0;

}

Step-by-step explanation:

Create a string array and store roman number equivalent of 1 to 10 in it. Take input from user between 1 to 20 only.If the input is greater than 20 or less than 0 then it again ask for the valid input and if the input is less or equal to 10 then find the equivalent roman number form then string array and print it. And if the input is greater than 10 and less or equal to 20 then it subtract 10 from the input and find the equivalent to the remainder and append "X" before the roman number.This process continue till user enter 0 as input. Here 0 is used to end the program.

Output:

Enter a number and between 1 and 20

Enter '0' to exit program: 24

Please Enter number between 1-20 only:

Enter a number and between 1 and 20

Enter '0' to exit program: -3

Please Enter number between 1-20 only:

Enter a number and between 1 and 20

Enter '0' to exit program: 5

V

Enter a number and between 1 and 20

Enter '0' to exit program: 7

VII

Enter a number and between 1 and 20

Enter '0' to exit program: 0

User Nwly
by
8.4k points