Python program to convert integer to roman


Roman numerals are a numeral system that originated in ancient Rome. They were widely used throughout the Roman Empire and continue to be used in certain contexts today. Roman numerals are based on a combination of letters from the Latin alphabet to represent numeric values.

The basic symbols used in Roman numerals are as follows −

I: 1
V: 5
X: 10
L: 50
C: 100
D: 500
M: 1000

To form larger numbers, these symbols are combined in various ways. The following rules apply −

  • Symbols are typically written from left to right in descending order of value.

  • If a smaller value symbol appears before a larger value symbol, the smaller value is subtracted. For example, IV represents 4 (5 - 1) and IX represents 9 (10 - 1).

  • If a smaller value symbol appears after a larger value symbol, the smaller value is added. For example, VI represents 6 (5 + 1) and XI represents 11 (10 + 1).

  • A symbol can be repeated up to three times in a row to represent the addition of that value. For example, III represents 3 (1 + 1 + 1), and XXX represents 30 (10 + 10 + 10).

  • A bar placed above a symbol multiplies its value by 1000. For example, VĖ„ represents 5000.

When converting an integer to a Roman numeral, the general process involves finding the largest symbol that is less than or equal to the given number and adding it to the Roman numeral representation. The process is repeated for the remainder until the entire number is converted.

In this article, we will see different approaches to converting an integer to a Roman numeral.

Approach

Following are the steps involved in converting an integer to a Roman numeral −

  • Define the Roman numeral symbols and their corresponding values. These symbols include I, V, X, L, C, D, and M, representing 1, 5, 10, 50, 100, 500, and 1000, respectively.

  • Start with an empty string to store the Roman numeral representation.

  • Iterate through the symbols from the largest to the smallest value.

  • Check if the current symbol's value is less than or equal to the given integer.

  • If it is, subtract the symbol's value from the integer and append the symbol to the Roman numeral string.

  • Repeat the above step while the current symbol's value is still less than or equal to the remaining integer.

  • Move on to the next symbol and repeat steps 5 and 6 until all symbols have been checked.

  • Return the Roman numeral string.

Using a List of Tuples

This approach utilizes a list to store the Roman numeral symbols along with their corresponding values represented as tuples. 

Example

Here's a Python program that converts an integer to its corresponding Roman numeral.

def integer_to_roman(num):
    symbols = [
        (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 = ''
    for value, symbol in symbols:
        while num >= value:
            roman += symbol
            num -= value

    return roman

# Define the input integer
number = 14
print('Input integer number:',number)

roman_numeral = integer_to_roman(number)
print(f"The Roman numeral representation of {number} is: {roman_numeral}")

Output

Input integer number: 14
The Roman numeral representation of 14 is: XIV

Using a Dictionary

This approach utilizes the Python dictionary data structure to store the Roman numeral symbols along with their corresponding values represented as key-value pairs. 

Example

Let's see the below example of converting an integer to its corresponding Roman numeral using a dictionary.

def integer_to_roman(num):
    symbols = {
        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 = ''
    for value in sorted(symbols.keys(), reverse=True):
        while num >= value:
            roman += symbols[value]
            num -= value

    return roman

# Define the input integer
number = 231
print('Input integer number:',number)

roman_numeral = integer_to_roman(number)
print(f"The Roman numeral representation of {number} is: {roman_numeral}")

Output

Input integer number: 231
The Roman numeral representation of 231 is: CCXXXI

Using the roman library

To convert integers to Roman numerals in Python, the roman library can be utilized. for that, we first need to install the library using the pip command (pip install roman). Once installed, the toRoman() function from the roman library can be employed to convert an integer to its corresponding Roman numeral.

Example

In this example, we will convert the number 123 to its Roman numeral representation using the toRoman() function.

# import the roman module
import roman

def integer_to_roman(num):
    return roman.toRoman(number)

# Define the input integer
number = 121
print('Input integer number:',number)

roman_numeral = integer_to_roman(number)
print(f"The Roman numeral representation of {number} is: {roman_numeral}")

Output

Input integer number: 121
The Roman numeral representation of 121 is: CXXI

Updated on: 29-Aug-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements