-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinttoroman.cpp
More file actions
40 lines (36 loc) · 773 Bytes
/
inttoroman.cpp
File metadata and controls
40 lines (36 loc) · 773 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<bits/stdc++.h>
using namespace std;
unordered_map<int, char> romanMap = {
{1000, 'M'},
{900, 'C'},
{500, 'D'},
{400, 'C'},
{100, 'C'},
{90, 'X'},
{50, 'L'},
{40, 'X'},
{10, 'X'},
{9, 'I'},
{5, 'V'},
{4, 'I'},
{1, 'I'}
};
string intToRoman(int num) {
int prev = 0;
string s = "";
vector<int> values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
for (int value : values) {
while (num >= value) {
s += romanMap[value];
num -= value;
}
}
return s;
}
int main() {
int num;
cout << "Enter number: ";
cin >> num;
cout << "roman value: " << intToRoman(num) << endl;
return 0;
}