The Program in C++ Program to Convert Given integer to a roman numeral is given below:
#include <iostream>
#include <string>
using namespace std;
string intToRoman(int num) {
string symbol[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int value[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
string roman;
for (int i = 0; num != 0; i++) {
while (num >= value[i]) {
num -= value[i];
roman += symbol[i];
}
}
return roman;
}
int main() {
int num;
cout << "Hello Codeuari Family,enter an integer here to find the Roman Numeral: ";
cin >> num;
cout << "Okay, the Roman numeral is: " << intToRoman(num) << endl;
return 0;
}
Output:
Hello Codeuari Family,enter an integer here to find the Roman Numeral: 2023
Okay, the Roman numeral is: MMXXIII
Pro-Tips💡
In this program, the intToRoman
function converts an integer to a Roman numeral by using arrays of symbols and values for the symbols and looping through the values,
subtracting the value from the integer and appending the corresponding symbol to a string, until the integer is zero.
The main
function takes the user input for the integer and outputs its Roman numeral by calling the intToRoman
function.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.