Roman Number

See the original problem on HackerRank.

Given a decimal number N between 1 and 4999, find its corresponding Roman numeral.

Roman numerals are formed by the combination of these letters:

1
2
3
4
5
6
7
I=1
V=5
X=10
L=50
C=100
D=500
M=1000

Rules to combine letters:

  1. A letter repeats its value that many times (XXX = 30, CC = 200, etc.). A letter can only be repeated three times (except M).
  2. If one or more letters are placed after another letter of greater value, add that amount (VI = 6 (5+1)).
  3. If a letter is placed before another letter of greater value, subtract that amount (IV = 4(5-1)).

Input Format

The only input is N.

Constraints

1
1 <= N <= 4999

Output Format

The corresponding roman number.

Solutions

Wait! Have you challenged yourself with this problem? If yes, click here to show the solution.
We've worked on this challenge in these gyms: modena  padua  milan