0
0
PythonProgramBeginner · 2 min read

Python Program to Convert Integer to Roman Numerals

Use a mapping of integer values to Roman numeral symbols and repeatedly subtract the largest possible value while appending its symbol, like in int_to_roman(num) function that loops through values and builds the Roman numeral string.
📋

Examples

Input3
OutputIII
Input58
OutputLVIII
Input1994
OutputMCMXCIV
🧠

How to Think About It

To convert an integer to Roman numerals, start from the largest Roman value and check if it fits into the number. If yes, add the Roman symbol and subtract that value from the number. Repeat this until the number becomes zero, moving to smaller values as needed.
📐

Algorithm

1
Create a list of tuples pairing Roman numeral symbols with their integer values, ordered from largest to smallest.
2
Initialize an empty string to store the Roman numeral result.
3
For each symbol and value in the list, while the input number is greater than or equal to the value, append the symbol to the result and subtract the value from the number.
4
Return the final Roman numeral string after processing all values.
💻

Code

python
def int_to_roman(num):
    val = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
    syms = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
    roman_num = ""
    for i in range(len(val)):
        while num >= val[i]:
            roman_num += syms[i]
            num -= val[i]
    return roman_num

print(int_to_roman(1994))
Output
MCMXCIV
🔍

Dry Run

Let's trace the input 1994 through the code

1

Start with 1994

num = 1994, roman_num = ""

2

Check 1000 (M)

1994 >= 1000, add 'M', num = 1994 - 1000 = 994, roman_num = 'M'

3

Check 900 (CM)

994 >= 900, add 'CM', num = 994 - 900 = 94, roman_num = 'MCM'

4

Check 500 (D)

94 < 500, skip

5

Check 400 (CD)

94 < 400, skip

6

Check 100 (C)

94 < 100, skip

7

Check 90 (XC)

94 >= 90, add 'XC', num = 94 - 90 = 4, roman_num = 'MCMXC'

8

Check 50 (L)

4 < 50, skip

9

Check 40 (XL)

4 < 40, skip

10

Check 10 (X)

4 < 10, skip

11

Check 9 (IX)

4 < 9, skip

12

Check 5 (V)

4 < 5, skip

13

Check 4 (IV)

4 >= 4, add 'IV', num = 4 - 4 = 0, roman_num = 'MCMXCIV'

Roman Numeral Built
M
MCM
MCMXC
MCMXCIV
💡

Why This Works

Step 1: Mapping values to symbols

The code uses two lists: one for integer values and one for Roman symbols, ordered from largest to smallest, to match parts of the number.

Step 2: Building the Roman numeral

It repeatedly subtracts the largest possible value from the number and adds the corresponding symbol to the result string.

Step 3: Looping through all values

The loop continues until the number is reduced to zero, ensuring all parts are converted correctly.

🔄

Alternative Approaches

Recursive approach
python
def int_to_roman_recursive(num):
    val = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
    syms = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
    if num == 0:
        return ""
    for i in range(len(val)):
        if num >= val[i]:
            return syms[i] + int_to_roman_recursive(num - val[i])
Uses recursion instead of loops; elegant but may be less efficient for large numbers.
Using dictionary and while loop
python
def int_to_roman_dict(num):
    roman_map = {1000: 'M', 900: 'CM', 500: 'D', 400: 'CD', 100: 'C', 90: 'XC',
                 50: 'L', 40: 'XL', 10: 'X', 9: 'IX', 5: 'V', 4: 'IV', 1: 'I'}
    roman_num = ""
    for value in sorted(roman_map.keys(), reverse=True):
        while num >= value:
            roman_num += roman_map[value]
            num -= value
    return roman_num
Uses a dictionary for mapping; slightly less direct but more flexible for modifications.

Complexity: O(n) time, O(1) space

Time Complexity

The algorithm loops through a fixed list of Roman numeral values and subtracts from the number, so it runs in linear time relative to the number of symbols added, which is proportional to the input size.

Space Complexity

Uses a fixed amount of extra space for the symbol lists and the output string, so space is constant relative to input size.

Which Approach is Fastest?

The iterative approach with lists is generally faster and more memory-efficient than recursion, which adds call stack overhead.

ApproachTimeSpaceBest For
Iterative with listsO(n)O(1)Fast and simple conversion
RecursiveO(n)O(n)Elegant code but less efficient
Dictionary mappingO(n)O(1)Flexible mapping, easy to modify
💡
Always start converting from the largest Roman numeral value to the smallest for correct results.
⚠️
Trying to convert by dividing digits individually without considering Roman numeral subtractive rules.