Write a program that accepts a year written as a four-digit Arabic (ordinary) numeral and outputs the year written in Roman numerals. Important Roman numerals are V for 5, X for 10, L for 50, C for 100, D for 500, and M for 1,000. Recall that some numbers are formed by using a kind of subtraction of one Roman “digit”; for example, IV is 4 produced as V minus I, XL is 40, CM is 900, and so on. A few sample years: MCM is
1900, MCML is 1950, MCMLX is 1960, MCMXL is 1940, MCMLXXXIX is
1989. Assume the year is between 1000 and 3000. Your program should
include a loop that lets the user repeat this calculation until the user says
she or he is done.
PROGRAM CODE:
#include <iostream>
#include <string>
using namespace std;
// Function to convert a single digit to its Roman numeral representation
string digitToRoman(int digit, char one, char five, char ten) {
string result;
if (digit == 9) {
result += one;
result += ten;
} else if (digit >= 5) {
result += five;
for (int i = 0; i < digit - 5; i++) {
result += one;
}
} else if (digit == 4) {
result += one;
result += five;
} else {
for (int i = 0; i < digit; i++) {
result += one;
}
}
return result;
}
// Function to convert the four-digit year to Roman numerals
string arabicToRoman(int year) {
string result;
// Extract each digit from the year
int thousands = year / 1000;
int hundreds = (year / 100) % 10;
int tens = (year / 10) % 10;
int ones = year % 10;
// Convert thousands digit
for (int i = 0; i < thousands; i++) {
result += 'M';
}
// Convert hundreds digit
result += digitToRoman(hundreds, 'C', 'D', 'M');
// Convert tens digit
result += digitToRoman(tens, 'X', 'L', 'C');
// Convert ones digit
result += digitToRoman(ones, 'I', 'V', 'X');
return result;
}
int main() {
int year;
// Loop until the user says they are done
while (true) {
// Get the year from the user
cout << "Enter a year between 1000 and 3000 (or -1 to exit): ";
cin >> year;
if (year == -1) {
// Exit the loop if the user enters -1
break;
}
// Check if the year is within the valid range
if (year >= 1000 && year <= 3000) {
// Convert the year to Roman numerals and display the result
string romanNumeral = arabicToRoman(year);
cout << year << " in Roman numerals is: " << romanNumeral <<endl;
} else {
cout << "Invalid year. Please enter a year between 1000 and 3000." <<endl;
}
}
return 0;
}
OUTPUT:
Enter a year between 1000 and 3000 (or -1 to exit): 1222
1222 in Roman numerals is: MCCXXII
Enter a year between 1000 and 3000 (or -1 to exit): 222
Invalid year. Please enter a year between 1000 and 3000.
Enter a year between 1000 and 3000 (or -1 to exit): 9999
Invalid year. Please enter a year between 1000 and 3000.
Enter a year between 1000 and 3000 (or -1 to exit): 2132
2132 in Roman numerals is: MMCXXXII
Enter a year between 1000 and 3000 (or -1 to exit): 50000
Invalid year. Please enter a year between 1000 and 3000.
Enter a year between 1000 and 3000 (or -1 to exit): 2999
2999 in Roman numerals is: MMCMXCIX
Enter a year between 1000 and 3000 (or -1 to exit): -1