Given a Romal numeral, find its corresponding decimal value.
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
A number in Roman Numerals is a string of these symbols written in descending order(e.g. M’s first, followed by D’s, etc.). However, in a few specific cases, to avoid four characters being repeated in succession (such as IIII or XXXX), subtractive notation is often used as follows:
- I placed before V or X indicates one less, so four is IV (one less than 5) and 9 is IX (one less than 10).
- X placed before L or C indicates ten less, so forty is XL (10 less than 50) and 90 is XC (ten less than a hundred).
- C placed before D or M indicates a hundred less, so four hundred is CD (a hundred less than five hundred) and nine hundred is CM (a hundred less than a thousand).
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 current value of symbol is greater than or equal to the value of next symbol, then add this value to the running total.
- else subtract this value by adding the value of next symbol to the running total.
Following is the implementation of the above algorithm:
Integer form of Roman Numeral is 1904
This article is contributed by Rahul Agrawal .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Converting Decimal Number lying between 1 to 3999 to Roman Numerals
- Converting one string to other using append and delete last operations
- Program for hexadecimal to decimal
- Convert Binary fraction to Decimal
- Nth character in Concatenated Decimal String
- Program for Binary To Decimal Conversion
- Recursive Program for Binary to Decimal
- Program for Decimal to Binary Conversion
- Decimal to binary number using recursion
- Program for octal to decimal conversion
- StringStream in C++ for Decimal to Hexadecimal and back
- Program for decimal to octal conversion
- Decimal representation of given binary string is divisible by 10 or not
- Convert from any base to decimal and vice versa
- Decimal representation of given binary string is divisible by 20 or not