Ctrl K

Number in Roman Numerals

Medium
Roman numerals use I = 1, V = 5, X = 10, L = 50, C = 100, D = 500 and M = 1000. A number is written place by place, from the highest decimal place down, and each place is encoded on its own: - digits 1–3 repeat the place's unit symbol (I, X, C or M): 3 = III, 30 = XXX; - digits 5–8 use the place's "five" symbol (V, L, D) followed by unit symbols: 7 = VII, 60 = LX; - digits 4 and 9 are written by subtraction, with the unit symbol placed before the "five" or the "ten" of the same place: 4 = IV, 9 = IX, 40 = XL, 90 = XC, 400 = CD, 900 = CM. Given an integer number, return it in Roman numerals.
Example 1
number=2468 → 'MMCDLXVIII'

2000 = MM, 400 = CD, 60 = LX, 8 = VIII.

Example 2
number=94 → 'XCIV'

90 = XC, 4 = IV. IC is not allowed: subtraction only works within one decimal place.

Example 3
number=7 → 'VII'

5 + 1 + 1 = VII.

Constraints
  • 1 ≤ number ≤ 3999
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFraction as a Decimal MediumDigits From Scrambled Words MediumPieces With Matching Ends MediumMessages Behind Key Presses MediumPieces With a Fixed Zero-to-One Ratio Medium
loading Python…
Input
number=2468
Expected
'MMCDLXVIII'

Run checks the first 3 tests in your browser. Submit checks all 80 tests on the server, hidden ones included.