-
Notifications
You must be signed in to change notification settings - Fork 368
/
Roman_to_Decimal.cpp
68 lines (67 loc) · 1.6 KB
/
Roman_to_Decimal.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*Name : Atul Kumar
Github username : atul1510
Repositary name : Algorithms
*/
/* Problem Statement : Given a Roman numeral, the task is to convert it into its decimal equivalent.
EXAMPLES :
1)
Input: "XXI"
Output: 21
2)
Input: "MCMXCIV"
Output: 1994 */
#include <iostream>
#include <map>
#include <string>
using namespace std;
// Function to return the decimal equivalent of a Roman numeral
int value(char c)
{
if (c == 'I')
return 1;
if (c == 'V')
return 5;
if (c == 'X')
return 10;
if (c == 'L')
return 50;
if (c == 'C')
return 100;
if (c == 'D')
return 500;
if (c == 'M')
return 1000;
// If the character is not a valid Roman numeral
return -1;
}
// Function to convert Roman numeral to decimal
int romanToDecimal(string str)
{
int result = 0;
int prev = 0;
// Iterating over each character in the input string in reverse order
for (int i = str.length() - 1; i >= 0; i--)
{
int curr = value(str[i]);
if (curr >= prev)
{
// If the value of the current symbol is greater than or equal to the value of the previous symbol, add it to the result
result += curr;
}
else
{
// If the value of the current symbol is less than the value of the previous symbol, subtract it from the result
result -= curr;
}
prev = curr;
}
return result;
}
int main()
{
string str;
cout << "Enter a Roman numeral: ";
cin >> str;
cout << "Decimal equivalent: " << romanToDecimal(str) << endl;
return 0;
}