Question

In: Computer Science

Write a program in C++ that converts a positive integer into the Roman number system. The...

Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits

I      1

V    5

X    10

L     50

C     100

D    500

M    1,000

Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as

I             1

II            2

III        3

IV        4

V           5

VI        6

VII        7

VIII       8

IX        9

As you can see, an I preceding a V or X is subtracted from the value, and you can never have more than three I’s in a row. (4) Tens and hundreds are done the same way, except that the letters X, L, C and C, D, M are used instead of I and V, X, respectively.

Solutions

Expert Solution

Explanation:

Lets understand this problem with an example. Let number = 78

then largest base value that can divide 78 is 50. division = 78/50 = 1

So it will print 'L' 1 time.

number = 78 % 50 = 28

then largest base value that can divide 28 is 10. division = 28/10 = 2

So it will print 'X' 2 times.

number = 28 % 10 = 8

then largest base value that can divide 8 is 5. division = 8/5 = 1

So it will print 'V' 1 time.

number = 8 % 5 = 3

then largest base value that can divide 3 is 1. division = 3/1 = 3

So it will print 'I' 3 times.

number = 3 % 1 = 0

Code:

#include <iostream>
using namespace std;

int main()
{
// declare a integer variable number
int number;
// Ask user to enter the number until the number is valid(1 - 3999)
do {
cout << "Enter a number between 1 and 3999: ";
cin >> number;
}while(number < 1 || number > 3999);
  
// Initialize an array of string which stores the roman value for nearest bases
string roman[] = {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
// Initialize an array of integer which store the corresponding value of bases
int value[] = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
  
// i is the index value of last element of array
int i = sizeof(value)/sizeof(value[0]) - 1;
// Loop until the value of number is greater than 0
while(number > 0) {
// divide the number with ith element of array value array
// Find largest base value for number
int division = number / value[i];
// Store the value of number after removing the base digit
number = number % value[i];
// Print the corresponding roman value of base division times
while(division) {
cout << roman[i];
division--;
}
i--;
}
return 0;
}

Screenshot of code:

Output:

If you're still having any doubt then please feel free to ask in the comment section.


Related Solutions

Write a program in C++ that converts a positive integer into the Roman number system. The...
Write a program in C++ that converts a positive integer into the Roman number system. The Roman number system has digits I      1 V    5 X    10 L     50 C     100 D    500 M    1,000 Numbers are formed according to the following rules. (1) Only numbers up to 3,999 are represented. (2) As in the decimal system, the thousands, hundreds, tens, and ones are expressed separately. (3) The numbers 1 to 9 are expressed as...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose...
Write a C++ program that accepts a positive integer number from the keyboard . The purpose of the program is to find and the display all the square pair numbers between 1 and that number. The user should be able to repeat the process until he/she enters n or N in order to terminate the process and the program. Square numbers are certain pairs of numbers when added together gives a square number and when subtracted also gives a square...
Write a C++ program that keeps asking user to enter a positive integer number until a...
Write a C++ program that keeps asking user to enter a positive integer number until a sentinel value (999) is entered. Then for each positive number entered by the user, find out how many digits it consists. (Hint: divide a number by 10 will remove one digit from the number. You can count how many divisions it needs to bring the number to 0.) An example of executing such a program is shown below. Note that the user input is...
In c++ Write a program that reads a string consisting of a positive integer or a...
In c++ Write a program that reads a string consisting of a positive integer or a positive decimal number and converts the number to the numeric format. If the string consists of a decimal number, the program must use a stack to convert the decimal number to the numeric format. Use the STL stack
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n...
Write a c++ program of the Fibonacci Sequence. Have the user enter a positive integer n and compute the nth Fibonacci number. The program should end when the user enters a number less than or equal to zero
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the...
Write a function decimalToBinary(n) that converts a positive decimal integer n to a string representing the corresponding binary number. Do the conversion by repeatedly dividing the number n by 2 using integer division, keepting track of the remainders, until the number is reduced to 0. The remainders written in reverse order form the binary number string. Do integer division of 5 by 2, so that N//2 is 2 with remainder 1. Now divide 2 by 2 to get 1 with...
Write a C++ program to read a collective of integer numbers. I f the number is...
Write a C++ program to read a collective of integer numbers. I f the number is greater than zero and less than 15 then terminate the loop and find factorial of the number
In C++, type a function function(int n, int base) that converts a positive integer x to...
In C++, type a function function(int n, int base) that converts a positive integer x to any base between 2 and 9. The function HAS to do this using a stack, and using methods from below: +isEmpty(): boolean +push(newEntry: ItemType): boolean +pop(): boolean +peek(): ItemType (Also, type a program to test the function). Hint: could use simple iteration continually divides the decimal number by base and keeps track of the remainder by using a stack.
c ++ program that converts from any base to a decimal number
c ++ program that converts from any base to a decimal number
Write a program that accepts a number of minutes and converts it both to hours and...
Write a program that accepts a number of minutes and converts it both to hours and days. For example, 6000 minutes is 100.0 hours or 4.166666666666667 days. (I currently have what is below) import java.util.Scanner; public class MinutesConversion { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numOfMinutes = sc.nextInt(); double hour = numOfMinutes/60.00; double days = (hour/24); System.out.println(numOfMinutes + " minutes is " + hour + " hours or " + days + " days.");...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT