Converting Decimal Number lying between 1 to 3999 to Roman Numerals in C++


In this tutorial, we will be discussing a program to convert decimal number lying between 1 to 3999 to roman numerals.

For this we will be provided with a random integer. Our task is to convert the given number into its roman numeral equivalent.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//converting decimal to roman numeral
int printRoman(int number){
   int num[] = {1,4,5,9,10,40,50,90,100,400,500,900,1000};
   string sym[] =
   {"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"};
   int i=12;
   while(number>0){
      int div = number/num[i];
      number = number%num[i];
      while(div--){
         cout<<sym[i];
      }
      i--;
   }
}
int main(){
   int number = 3949;
   printRoman(number);
   return 0;
}

Output

MMMCMXLIX

Updated on: 29-Jan-2020

264 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements