Converting Roman Numerals to Decimal between 1 to 3999



Example :

Input: IX
Output: 9

Input : XL
Output : 40

Input : MCMIV
Output: 1904
M is a thousand, CM is nine hundred
and IV is four


Roman numerals are based on below symbols.

SYMBOL       VALUE
I             1
IV            4
V             5
IX            9
X             10
XL            40
L             50
XC            90
C             100
CD            400
D             500
CM            900
M             1000     


The algorithm to convert Roman Numerals to Integer Number:

Split the Roman Numeral string into Roman Symbols (character).
Convert each symbol of Roman Numerals into the value it represents.
Take symbol one by one from starting from index 0:
If the current value of the symbol is greater than or equal to the value of the next symbol, then add this value to the running total.
else subtract this value by adding the value of the next symbol to the running total.

Sources


Simple Code:






Post a Comment

0 Comments