Objective: Given a Roman number, write a program to convert it to Integer.

Roman Number –   Letters used in Roman numerals and the corresponding numerical values are given in the table below.

Rules: 

Roman numerals are usually written in highest to lowest from left to right except for some special cases where left character is less than the right character. for example ‘IV’ is equivalent to 4 not ‘IIII’. In such cases, subtract the left character value from the right character value. ‘IV’ will be 5-1 = 4, same for ‘IX’ = 10-1 = 9. Below are the cases –

  • I can be placed before V or X, represents subtract one, so IV (5-1) = 4 and 9 is IX (10-1)=9.

  • X can be placed before L or C represents subtract ten, so XL (50-10) = 40 and XC (100-10)=90.

  • C placed before D or M represents subtract hundred, so CD (500-100)=400 and CM (1000-100)=900.

Example:

Roman: XXV
Integer: 25
---------------------------------------------------
Roman: XXXVI
Integer: 36
---------------------------------------------------
Roman: MXXIII
Integer: 1023
---------------------------------------------------
Roman: DXLII
Integer: 542

Attachments: