How to Convert Arabic Numerals to Roman Numerals with Python

How to Convert Arabic Numerals to Roman Numerals with Python

In this article, I will show you how to implement a simple Python program to convert an Arabic (decimal) number to a Roman numeral.

Converting Arabic numerals to Roman numerals is a very common exercise for those who are learning to program and want to familiarize themselves with basic control structures and algorithms. In this article, I will show you how to implement a simple Python program to convert an Arabic (decimal) number to a Roman numeral.

First, a brief introduction to Roman numerals. The main symbols are:

SymbolValue
I1
V5
X10
L50
C100
D500
M1000

The main rules for conversion are:

  • A larger symbol preceding a smaller one is added (e.g. VI = 6, XV = 15).
  • A smaller symbol preceding a larger one is subtracted (e.g. IV = 4, IX = 9).

The logic of converting Arabic numerals to Roman numerals can be described in these steps:

  1. We create a map of all Roman values ​​to their Arabic equivalents.
  2. We iterate over the map starting from the largest Arabic number and continue subtracting from the Arabic number as many times as possible, adding the corresponding Roman symbol to the output string.

# Define a function for the conversion
def int_to_roman(num):
  # Create a Roman-Arabic numeral conversion map
  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')
  ]

  # Empty string that will hold the result
  roman_numeral = ""

  # Let's iterate over the map
  for value, symbol in roman_map:
    # Let's find how many times we can subtract the current value
    while num >= value:
      roman_numeral += symbol # Let's add the corresponding roman symbol
      num -= value # Let's subtract the value from the arabic number

  return roman_numeral

# Testing the function
arabic_number = 1987
roman_number = int_to_roman(arabic_number)
print(f"The number {arabic_number} in roman numerals is: {roman_number}")

Code Explanation:

  1. Roman Numeral Map: The variable roman_map is a list of tuples, where each tuple represents an Arabic number and its corresponding Roman symbol. The descending order is essential because we need to start from the largest number to correctly construct the Roman number.

  2. Iterating over the Map: We use a for loop to iterate through each Roman value in the map. The inner while loop allows us to add the Roman symbol as long as the Arabic number is greater than or equal to the associated value.

  3. Result Construction: Each time we subtract a value from the Arabic number, we add the corresponding Roman symbol to the roman_numeral string. At the end of the iteration, the string will contain the correct Roman numeral.

Considerations:

  1. Number ranges: Classical Roman numerals are mainly used to represent numbers up to a few thousand. With our approach, we can handle numbers up to about 3999. If you wanted larger numbers, you should consider using a different notation (such as Roman numerals with slashes to indicate multiplications by 1000).

  2. Negative values ​​and zero: Roman numerals do not have a representation for zero or negative numbers. Therefore, it is advisable to add a check to handle these cases.

Conclusion

In this article, we have seen how to convert an Arabic numeral to a Roman numeral using Python. This type of problem is useful for practicing control structures, loops, and lists. It is also a great example of how to approach string manipulation problems and algorithm building.