C++ Convert an Integer to Roman Numeral
Posted by Samath
Last Updated: January 20, 2024

This is a program that ask the user for a integer and convert it to roman numeral, it is written using the C++ programming language. 

#include <iostream>
#include <string>

using namespace std;

string roman_to_int(int value){
  struct romanStruct { int value; char const* numeral; };
  static romanStruct const romandata[] =
     { 1000, "M",
        900, "CM",
        500, "D",
        400, "CD",
        100, "C",
         90, "XC",
         50, "L",
         40, "XL",
         10, "X",
          9, "IX",
          5, "V",
          4, "IV",
          1, "I",
          0, NULL }; 
 
  string result;
  for (romanStruct const* current = romandata; current->value > 0; ++current){
    while (value >= current->value){
      result += current->numeral;
      value  -= current->value;
    }
  }
  return result;
}


int main(){
  int i=-1;
  
  do{
    cout << "Enter Integer [1-3999]: ";
    cin >> i;
    if(i==0){
       cout << "Error";
    }else{
      cout << roman_to_int(i) << endl;
    }
    
  }while(i!=0);
}