Converting Decimal Number to Roman Numerals between 1 to 3999


Given a number, find its corresponding Roman numeral. Examples:
Input: 9
Output: IX

Input : 40
Output : XL

Input:    1904
Output : MCMIV

Following is the list of Roman symbols which include subtractive cases also:

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 


Simple Input:    3549
Simple Output:  MMMDXLIX


Explanation:
    Step 1
  • Initially number = 3549
  • Since 3549 >= 1000 ; largest base value will be 1000 initially.
  • Divide 3549/1000. Quotient = 3, Remainder =549. The corresponding symbol Mwill be repeated thrice.
  • Step 2
  • Now, number = 549
  • 1000 > 549 >= 500 ; largest base value will be 500.
  • Divide 549/500. Quotient = 1, Remainder =49. The corresponding symbol D will be repeated once.
  • Step 3
  • Now, number = 49
  • 50 > 49 >= 40 ; largest base value is 40.
  • Divide 49/40. Quotient = 1, Remainder = 9. The corresponding symbol XL will be repeated once.
  • Step 4
  • Now, number = 9
  • 10> 9 >= 9 ; largest base value is 9.
  • Divide 9/9. Quotient = 1, Remainder = 0. The corresponding symbol IX will be repeated once.
  • Step 5
  • Finally, the number becomes 0, the algorithm stops here.
  • The output obtained MMMDXLIX.


Simple Code:







Post a Comment

0 Comments