How to convert an integer into a Roman numeral

Key takeaways:

  • Roman numerals use Latin letters to represent numbers, combining both additive and subtractive principles. Key values include I (1), V (5), X (10), L (50), C (100), D (500), and M (1000), with combinations like IV (4), IX (9), and others for subtraction.

  • To convert an integer, iterate through a lookup table of Roman numerals from largest to smallest. Divide the number by each numeral’s value, append the corresponding symbol, and use modulus to update the number.

The Integer to Roman problem is a classic algorithmic problem that involves converting an integer into its corresponding Roman numeral representation.

What are Roman numerals?

Roman numerals, originating in ancient Rome, were widely used across Europe until the Late Middle Ages and are still in use today in various contexts. Each numeral corresponds to a specific value, represented by a combination of Latin letters. They are explained in the table below:

Letter

Value

I

1

V

5

X

10

L

50

C

100

D

500

M

1000

The number “3” is written as III in Roman numerals, just three ones added together. The number “12” is written as XII, simply X + II. The number 27 is written as XXVII, which is XX + V + II, and so on.

The system also incorporates subtraction principles, where certain combinations like “IV” represent 4 (5 - 1) and “IX” represent 9 (10 - 1). Similarly, there are combinations for numbers like 40 (XL) and 90 (XC), making the system more complex than a simple additive representation. This will make our table look like this:

Letter

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

The conversion algorithm

We can convert an integer to a Roman numeral by combining division and modulus. Let’s explain the algorithm with the example of the number “1400”.

  1. Firstly, we’ll iterate through our lookup table from the largest number to the smallest number.

  2. We’ll check how many times the current Roman numeral can fit into the given number. We will do this by dividing the number with the value of the Roman numeral from the lookup table. The result of the division will give us the quotient, which will be the number of times that particular numeral will be present in the final result.

  3. We multiply the division result with the Roman numeral and append the result to a string that will carry our final output.

  4. Finally, we will take the modulus of the number with the corresponding Roman numeral, subtracting the largest possible unit from the number and comparing the remainder to the rest of the table. We will continue this until the remainder is 0.

Converting 36 to roman value
Converting 36 to roman value

Converting an integer into a Roman numeral in Python

Let’s turn this algorithm into a Python code:

def intToRoman(num):
LookupTable = [["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]]
result = ""
for symbol, value in reversed(LookupTable):
if num // value:
count = num//value
result = result + (symbol * count)
num = num % value
return result
print(intToRoman(2467))

Let’s explain the code line by line:

  • Lines 2–4: Here, we initialize a list of lists called LookupTable. This is a list of lists where each inner list contains a Roman numeral symbol and its corresponding integer value.

  • Line 6: Here, we initialize the result variable, which will store our resultant string.

  • Line 7: Here, we initiate a loop that will iterate through the LookupTable. We iterate in reverse order, as it is important to start from the largest number.

  • Lines 8–11: We check if the value from the LookupTable can divide the the num evenly. If it can, then we store the result in the count variable to see how many times that particular symbol will appear in the result string. We then multiply the symbol we got from the LookupTable and multiply with the count variable to and add append it to the result variable. Finally, we update the num variable by taking the modulus of it with the value variable.

Conclusion

Converting an integer to a Roman numeral is a straightforward process when utilizing a structured lookup table and basic division and modulus operations. By iterating from the largest Roman numeral value downwards, you can efficiently build the corresponding numeral representation. This method ensures that both additive and subtractive Roman numeral principles are respected, providing an accurate and optimized conversion.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How would you write 25 as a Roman numeral?

25 is written as XXV in Roman numerals.


What is XXXIII in Roman numerals?

XXXIII is 33 in Roman numerals.


What is 0 in Roman numerals?

The concept of zero did not exist in ancient Roman numerals, so there is no symbol for 0 in Roman numeral notation.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved