diff --git a/Roman number to decimal b/Roman number to decimal new file mode 100644 index 00000000000..e1cf393848e --- /dev/null +++ b/Roman number to decimal @@ -0,0 +1,40 @@ + +def value(r): + if (r == 'I'): + return 1 + if (r == 'V'): + return 5 + if (r == 'X'): + return 10 + if (r == 'L'): + return 50 + if (r == 'C'): + return 100 + if (r == 'D'): + return 500 + if (r == 'M'): + return 1000 + return -1 + +def romanToDecimal(str): + res = 0 + i = 0 + + while (i < len(str)): + s1 = value(str[i]) + if (i + 1 < len(str)): + s2 = value(str[i + 1]) + if (s1 >= s2): + res = res + s1 + i = i + 1 + else: + res = res + s2 - s1 + i = i + 2 + else: + res = res + s1 + i = i + 1 + + return res + +romannumber = input("Enter the Roman Number you want to convert: ") +print("Integer form of Roman Numeral is: " + str(romanToDecimal(romannumber)))