Convert to Roman No | Practice | GeeksforGeeks

Given an integer n, your task is to complete the function convertToRoman which prints the corresponding roman number of n. Various symbols and their values are given below.
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
 
Example 1:
Input:
n = 5
Output: V

 
Example 2:
Input:
n = 3
Output: III

 
Your Task:
Complete the function convertToRoman() which takes an integer N as input parameter and returns the equivalent roman. 
 
Expected Time Complexity: O(log10N)
Expected Auxiliary Space: O(log10N * 10)
 
Constraints:
1<=n<=3999 
0
akjha4982 days ago
 
 C++ Code 
string convertToRoman(int n) {
        // code here
        int key[13]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
        string val[13]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        string ans="";
        for(int i=0;i<13;i++){
            while(key[i]<=n){
                ans+=val[i];
                n-=key[i];
            }
        }
        return ans;
    }
0
anamjamadar4641 week ago
java code:
class Solution{String convertToRoman(int n) { String roman="";while(n>=1000){    roman+="M";    n=n-1000;}       if(n>=900){    roman+="CM";    n=n-900;}while(n>=500){    roman+="D";    n=n-500;   }   if(n >= 400){       roman = roman + "CD";        n= n - 400;       }while(n>=100){    roman+="C";    n=n-100;}          if(n >= 90){           roman = roman + "XC";           n = n - 90;       }while(n>=50){    roman+="L";    n=n-50;       }        if(n >= 40){           roman = roman + "XL";           n = n- 40;       }while(n>=10){    roman+="X";    n=n-10;}         if(n == 9){           roman = roman + "IX";           n = n - 9;       }while(n>=5){    roman+="V";    n=n-5;}          if(n == 4)   {   roman = roman + "IV";   n = n - 4;       }       while(n>=1){    roman=roman+"I";    n=n-1;}return roman;}}
0
visitant1 week ago
string convertToRoman(int n) {
        // code here
        string romNum = "";
        if(n > 3999)
        {
            return "";
        }
        do
        {
            int num = getMSDigit(n);
            romNum = romNum + convertRoman(num, n/num);
            n = n%num;
        }while(n > 0);
        
        
        cout << romNum;
    }
    
    
    int getMSDigit(int n)
    {
        if(n>=10)
            return 10*getMSDigit(n/10);
        else
            return 1;
    }
    
    string convertRoman(int n, int val)
    {
        string ret = "";
        switch(val)
        {
            case 1:
            {
                ret = ret + getRoman(n);
                break;
            }
            case 2:
            {
                ret = ret + getRoman(n) + getRoman(n);
                break;
            }
            case 3:
            {
                ret = ret + getRoman(n) + getRoman(n) + getRoman(n);
                break;
            }
            case 4:
            {
                ret = ret + getRoman(n) + getRoman(5*n);
                break;
            }
            case 5:
            {
                ret = ret + getRoman(5*n);
                break;
            }
            case 6:
            {
                ret = ret + getRoman(5*n) + getRoman(n);
                break;
            }
            case 7:
            {
                ret = ret + getRoman(5*n) + getRoman(n) + getRoman(n);
                break;
            }
            case 8:
            {
                ret = ret + getRoman(5*n) + getRoman(n) + getRoman(n) + getRoman(n);
                break;
            }
            case 9:
            {
                ret = ret + getRoman(n) + getRoman(n*10);
                break;
            }
        }
        return ret;
    }
    
    string getRoman(int val)
    {
        switch(val)
        {
            case 1:
            {
                return "I";
            }
            case 5:
            {
                return "V";
            }
            case 10:
            {
                return "X";
            }
            case 50:
            {
                return "L";
            }
            case 100:
            {
               return "C";
            }
            case 500:
            {
                return "D";
            }
            case 1000:
            {
                return "M";
            }
        }
    }
};
0
tyagivt3 weeks ago
 //Simple java Solution//
//Simply to get the index value after dividation acc. to this a String will form//
String convertToRoman(int n) { String[] units={"","I","II","III","IV","V","VI","VII","VIII","IX"}; String[] tens={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"}; String[] hundreds{"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"}; String[] thousands={"","M","MM","MMM"}; String str=Integer.toString(n); String res="";return thousands[n/1000]+hundreds[(n%1000)/100]+tens[(n%100)/10]+units[(n%10)];}
0
dipawalimandaokar3212 months ago
What is the problem in this code,it's not passing all testcases 
class Solution{ public:   string convertToRoman(int n)    {      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(n>0)   {       int div=n/num[i];       int n=n%num[i];              while(div--)        {            return sym[i];        }           i--;   }   }};
+2
abhishek1999292 months ago
class Solution{ public:   string convertToRoman(int n) {       unordered_map<int, string> m;       m[1] = 'I';       m[4] = "IV";       m[9] = "IX";       m[5] = 'V';       m[10] = 'X';       m[40] = "XL";       m[50] = 'L';       m[90] = "XC";       m[100] = 'C';       m[400] = "CD";       m[500] = 'D';       m[900] = "CM";       m[1000] = 'M';              int arr[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};              string res;              for(int i=0; i<13; i++){           if(arr[i] <= n){               res += m[arr[i]];               n -= arr[i];               i--;           }       }       return res;    }};
0
abhixhek052 months ago
 string convertToRoman(int num) {
        // code here
         string res;
 string sym[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
 int val[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        //12 here sym string size
        for(int i=0; i<=12; i++)
        {
            while(num >= val[i])
            {
                res += sym[i];
                num -= val[i];
            }
        }
        
        return res;
    }
0
shailendramaheshwari172 months ago
//(C++ && Easy)

string convertToRoman(int n) {
       string s;
       while(n!=0)
       {
           if(n==4)
           { s+="IV"; n-=4; }
           else if(n==9)
           { s+="IX"; n-=9; }
           else if(n>=40 && n<50)
           { s+="XL"; n-=40; }
           else if(n>=90 && n<100)
           { s+="XC"; n-=90; }
           else if(n>=400 && n<500)
           { s+="CD"; n-=400; }
           else if(n>=900 && n<1000)
           { s+="CM"; n-=900; }
           else if(n==1 || n>1 && n<5)
           { s+='I'; n-=1; }
           else if(n==5 || n>5 && n<10)
           { s+='V'; n-=5; }
           else if(n==10 || n>10 && n<50)
           { s+='X'; n-=10; }
           else if(n==50 || n>50 && n<100)
           { s+='L'; n-=50; }
           else if(n==100 || n>100 && n<500)
           { s+='C'; n-=100; }
           else if(n==500 || n>500 && n<1000)
           { s+='D'; n-=500; }
           else if(n==1000 || n>1000)
           { s+='M'; n-=1000; }
       }
       return s;
   }
-1
amangaharwar11033 months ago
C++ Solution Using Map
I found the other solutions a bit lacking generality so only the map needs to be updated for higher numbers
Solution:
 
string convertToRoman(int n) {       // Creating a map to store basic roman numbers of decimal numbers       map<int,char> pr;       pr[1000] = 'M';       pr[500] = 'D';       pr[100] = 'C';       pr[50] = 'L';       pr[10] = 'X';       pr[5] = 'V';       pr[1] = 'I';       // Temp num as num will change       int num =n;       // Storing ans in this string       string ans="";       int i=0;       //While num is not 0       while(num)       {           // Temp string to store current roman number           string s="";           int d = (num%10);           // If digit 0 skip the case           if(d==0)           {               i++;               num = num/10;               continue;           }           d = d*pow(10,i);           num = num/10;           // If a direct match is found add that roman number           if(pr[d])           {               s+=pr[d];               i++;               ans = s+ans;               continue;           }           // If a round up solution like 4,9,40,90,400,900 is found then add both           if(pr[d+pow(10,i)])           {               s = s+pr[pow(10,i)];               s = s+pr[d+pow(10,i)];               i++;               ans = s+ans;               continue;           }           // If round up not found continue adding till d > 5*pow(10,i) 800 will add D and           // d will become 800-500 = 300           if(d>5*pow(10,i))           {               s = s + pr[5*pow(10,i)];               d = d-(5*pow(10,i));           }           // Eg 300 will add CCC            while(d>0)           {               d = d-pow(10,i);               s = s+pr[pow(10,i)];           }           i++;           ans = s+ans;       }       return ans;   }
 
 
-2
vivekdongare813 months ago
DID THAT ON A PURPOSE :)class Solution{String convertToRoman(int n) { int [] nums= {1000,500,100,50,10,5,1}; String ans=""; while(n>0){        // System.out.println(ans+" "+n);        if(n>=1000){ for(int i=0;i<n/1000;i++){  ans+="M";} n=n%1000; }      if(n>=900){ for(int i=0;i<n/900;i++){  ans+="CM";} n=n%900; }       if(n>=500){ for(int i=0;i<n/500;i++){  ans+="D";} n=n%500; }       if(n>=400){  for(int i=0;i<n/400;i++){  ans+="CD";} n=n%400; }       if(n>=100){  for(int i=0;i<n/100;i++){  ans+="C";} n=n%100; }       if(n>=90){  for(int i=0;i<n/90;i++){  ans+="XC";} n=n%90; }       if(n>=50){ for(int i=0;i<n/50;i++){  ans+="L";} n=n%50; }        if(n>=40){  for(int i=0;i<n/40;i++){  ans+="XL";} n=n%40; }       if(n>=10){  for(int i=0;i<n/10;i++){  ans+="X";} n=n%10; }       if(n>=9){  for(int i=0;i<n/9;i++){  ans+="IX";} n=n%9; }       if(n>=5){  for(int i=0;i<n/5;i++){  ans+="V";} n=n%5; }       if(n>=4){   for(int i=0;i<n/4;i++){  ans+="IV";} n=n%4; }       if(n>=1){   for(int i=0;i<n/1;i++){  ans+="I";} n=n%1; }      //   System.out.println(ans+" "+n); }return ans; }}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.