Write a C++ program to convert numbers into roman numerals
- User
Write a C++ program to convert numbers into roman numerals
- Assistant
Write a C++ program to convert numbers into roman numerals
Sure! Below is a C++ program that converts an integer to its corresponding Roman numeral. The program handles numbers from 1 to 3999, which is the typical range for standard Roman numerals.
Define Roman Numerals Mapping: We use two arrays, one for integer values and another for their corresponding Roman numerals. They are ordered from the highest value to the lowest to facilitate the conversion process.
Conversion Logic: We iterate through the integer values, subtracting as many of each value as possible from the input number while appending the corresponding Roman numeral to the result string each time.
Input Validation: The program checks if the input number is within the valid range (1 to 3999). If not, it notifies the user.
#include <iostream>
#include <string>
using namespace std;
// Function to convert integer to Roman numeral
string intToRoman(int num) {
// Arrays of Roman numerals and their corresponding integer values
int values[] = {1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4, 1};
string symbols[] = {"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV", "I"};
string roman = "";
for(int i = 0; i < 13; i++) {
while(num >= values[i]) {
num -= values[i];
roman += symbols[i];
}
if(num == 0)
break;
}
return roman;
}
int main() {
int number;
cout << "Enter an integer between 1 and 3999: ";
cin >> number;
// Input validation
if(number < 1 || number > 3999) {
cout << "Number out of range. Please enter a number between 1 and 3999." << endl;
return 1;
}
string romanNumeral = intToRoman(number);
cout << "Roman Numeral: " << romanNumeral << endl;
return 0;
}
Save the Code: Save the above code in a file named, for example, RomanNumeralConverter.cpp.
Compile: Use a C++ compiler like g++. Open your terminal or command prompt and navigate to the directory containing the file. Run the following command:
g++ -o RomanNumeralConverter RomanNumeralConverter.cpp
Run: Execute the compiled program:
./RomanNumeralConverter
(On Windows, you might run RomanNumeralConverter.exe instead.)
Input: When prompted, enter an integer between 1 and 3999 to see its Roman numeral representation.
Enter an integer between 1 and 3999: 1994
Roman Numeral: MCMXCIV
Arrays values and symbols: These arrays hold the integer values and their corresponding Roman numerals, ordered from highest to lowest. This ordering is crucial for the conversion logic.
Conversion Loop: For each value in the values array, the program subtracts it from num as many times as possible while appending the corresponding symbol to the roman string.
Early Termination: If num becomes zero, the loop breaks early to optimize performance.
Input Validation: Ensures that the user inputs a number within the acceptable range to prevent invalid conversions.
This program provides a straightforward and efficient way to convert integers to Roman numerals in C++. You can enhance it further by adding more features, such as handling larger numbers with overlines or allowing lowercase input.
Not yet included in a published release.