Roman to Integer

Practice this coding question on Coding Gurukul.

Problem statement

Convert a valid standard Roman numeral to its decimal value. Symbols I, V, X, L, C, D, M have values 1, 5, 10, 50, 100, 500, 1000. A smaller symbol before a larger one is subtracted instead of added. The only subtractive pairs are IV, IX, XL, XC, CD, and CM. The input is guaranteed to be a valid numeral representing a number from 1 to 3999.

Input format

One line: the Roman numeral.

Output format

Print its integer value.

Constraints

1 <= numeral length <= 15; valid standard Roman numerals from I to MMMCMXCIX.

Sample input

MCMXCIV

Sample output

1994

Sign in to run or submit a solution in C++, Java or Python.