Skip to content

Commit 5b8c716

Browse files
authored
Merge pull request #225 from ShreyaShukla03/patch-1
Create NumbertoRoman.cpp
2 parents 088d6b6 + 475e52b commit 5b8c716

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

Coding/NumbertoRoman.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
string intToRoman(int num) {
4+
map<int,string>mp;
5+
mp[1]="I";
6+
mp[4]="IV";
7+
mp[5]="V";
8+
mp[9]="IX";
9+
mp[10]="X";
10+
mp[40]="XL";
11+
mp[50]="L";
12+
mp[90]="XC";
13+
mp[100]="C";
14+
mp[400]="CD";
15+
mp[500]="D";
16+
mp[900]="CM";
17+
mp[1000]="M";
18+
int len=0,z=num;
19+
while(z!=0){
20+
z/=10;
21+
len++;
22+
}
23+
z=num;
24+
string ans="";
25+
for(int i=len-1;i>=0;i--){
26+
int div=pow(10,i);
27+
int req=z/div;
28+
// cout<<"z "<<z<<" req "<<req<<endl;
29+
int val=req*div;
30+
// cout<<"val "<<val<<" div "<<div<<endl;
31+
if(mp[val]!="")ans+=mp[val];
32+
else{
33+
if(div==1&&req>=5){
34+
ans+=mp[5];
35+
req-=5;
36+
}
37+
if(div==10&&req>=5){
38+
ans+=mp[50];
39+
req-=5;
40+
}
41+
if(div==100&&req>=5){
42+
ans+=mp[500];
43+
req-=5;
44+
}
45+
while(req--)ans+=mp[div];
46+
}
47+
z=z-val;
48+
}
49+
50+
return ans;
51+
}
52+
int main() {
53+
string z=intToRoman(1994);
54+
cout<<"The required conversion of Integer to Roman "<<z<<endl;
55+
}

0 commit comments

Comments
 (0)