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.

Can this be done in C++ without arrays

Solutions

Expert Solution

SOLUTION-
I have solve the problem in C++ code with comments and screenshot for easy understanding :)

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 HAVE ANY DOUBT PLEASE COMMENT DOWN BELOW I WILL SOLVE IT FOR YOU:)
----------------PLEASE RATE THE ANSWER-----------THANK YOU!!!!!!!!----------


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 java program to convert a positive integer value to a roman number. Your program...
Write a java program to convert a positive integer value to a roman number. Your program should print a description, prompt them for how many numbers they need to convert and then loop that many times. Each time the program needs to prompt for a number and output the equivalent roman numeral. If the user enters a number less than 1 then output an error message. See sample output below: **************************************************** * Welcome to the Roman Numeral Converter!          * *...
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...
Exercise 1: Write a program that converts a number entered in Roman numerals to decimal. Your...
Exercise 1: Write a program that converts a number entered in Roman numerals to decimal. Your program should consist of a class, say, Roman. An object of type Roman should do the following: Store the number as a Roman numeral. Convert and store the number into decimal. Print the number as a Roman numeral or decimal number as requested by the user. The decimal values of the Roman numerals are: M 1000 D 500 C 100 L 50 X 10...
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
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.
Create a C++ program that will prompt the user to input an positive integer number and...
Create a C++ program that will prompt the user to input an positive integer number and output the corresponding number to words. Check all possible invalid input data. (Please use only switch or if-else statements. Thank you.)
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 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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT