Converting Integers to Roman Numerals Using Python

Converting Integers to Roman Numerals Using Python

ECX 30 Days of Code and Design

Day 16

When in Rome

Task

  • Write a function that takes integers as input, and returns Roman numerals.
  • Using the aforementioned function, write a program that takes user input and prints out the Roman numeral form. This program must include all necessary type checks or exception handling.

My Approach

The above task is answered using two different approach in this article.

First Approach

This is the full code. I would explain it in bits throughout this article.

decimal = [1000000, 900000, 500000, 400000, 100000, 90000, 50000, 40000, 10000, 9000, 5000, 4000, 1000, 900, 500, 400,
           100, 90, 50, 40, 10, 9, 5, 4, 1]
roman_sym = ['M̄', 'C̄M̄', 'D̄', 'C̄D̄', 'C̄', 'X̄C̄', 'L̄', 'X̄L̄', 'X̄', 'MX̄', 'V̄', 'MV̄', 'M', 'CM', 'D', 'CD',
             'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']


# Function to convert integer to roman numerals
def decimal_to_roman(user_inputs):
    """Convert integers to roman numerals"""

    count = 0               # To iterate the decimal and roman_sym lists

    if user_inputs == 0:
        print('Nulla')

    while user_inputs > 0:
        # To get the quotient of each division (number // list integers)
        division = user_inputs // decimal[count]

        # Assign the input to have the remaining value after the symbol
        user_inputs = user_inputs % decimal[count]

        # We can use divmod() to get the quotient and remainder instead of the above.
        # division, user_inputs = divmod(user_inputs, decimal[count])

        # Quotient * the current symbol (string multiplication)
        roman_no = division * roman_sym[count]

        print(roman_no, end='')

        count = count + 1


print(' Integers to Roman Numerals '.center(40, '*'))

# Handle value errors
try:
    user_input = int(input('Enter a number to convert to Roman numerals: '))
    decimal_to_roman(user_input)
except ValueError:
    print('Invalid input. Enter an integer.')

First, we create two lists, one containing values of the Roman numerals and the second, their corresponding decimals in order.

decimal = [1000000, 900000, 500000, 400000, 100000, 90000, 50000, 40000, 10000, 9000, 5000, 4000, 1000, 900, 500, 400,
           100, 90, 50, 40, 10, 9, 5, 4, 1]
roman_sym = ['M̄', 'C̄M̄', 'D̄', 'C̄D̄', 'C̄', 'X̄C̄', 'L̄', 'X̄L̄', 'X̄', 'MX̄', 'V̄', 'MV̄', 'M', 'CM', 'D', 'CD',
             'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I']

Next, we define the function, decimal_to_roman(), which converts integers to Roman numerals. We then create a counter which we would use to iterate through the list. Next, we would check if the user inputs zero, if yes, we print “Nulla.”

def decimal_to_roman(user_inputs):
    """Converts an integer to roman numerals"""

    count = 0

    if user_inputs == 0:
        print('Nulla')

While the user's input is greater than zero, we would divide the user’s input by each integer in the decimal list starting from the highest value (first index) to the lowest (last index). We get the quotient and multiply it by the corresponding Roman numeral symbol. We then reassign the user input to have the remainder value and continue the same process of division. If the user inputs 3456, the process is as follows;

3456/1000 = 3 R 456
3 × M (1000) = MMM
456/400 = 1 R 56
1 × CD (400) = CD
56/50 = 1 R 6
1 × L (50) = L
6/5 = 1 R 1
5 × V (5)=V
1 = I
⸫ 3456 = MMMCDLVI

We can also perform the above simply by using the divmod() function because it calculates both the quotient and the remainder.

while user_inputs > 0:
    division = user_inputs // decimal[count]

    user_inputs = user_inputs % decimal[count]

    # division, user_inputs = divmod(user_inputs, decimal[count])

    roman_no = division * roman_sym[count]

    print(roman_no, end='')

    count = count + 1

Finally, we ask the user to input the value he wants to convert to Roman numerals.

print(' Integers to Roman Numerals '.center(40, '*'))

try:
    user_input = int(input('Enter a number to convert to Roman numerals: '))
    decimal_to_roman(user_input)
except ValueError:
    print('Invalid input. Enter an integer.')

Another Approach

This approach is less compact, but it does the work. If and while statements are used to do the checks. We start from the highest value and move to the lowest.

macron = '\u0304'               # For the dash above numerals corresponding to digits above 4000


def integer_to_roman(number):
    """Converts an integer to Roman numerals"""

    roman = ''          # For storing the Roman numerals

    if number == 0:
        print('Null')

    while number >= 1000000:
        number = number - 1000000
        roman = roman + 'M' + macron

    if number >= 900000:
        number = number - 900000
        roman = roman + 'C' + macron + 'D' + macron

    if number >= 500000:
        number = number - 500000
        roman = roman + 'D' + macron

    if number >= 400000:
        number = number - 400000
        roman = roman + 'C' + macron + 'D' + macron

    while number > 100000:
        number = number - 100000
        roman = roman + 'C' + macron

    if number >= 90000:
        number = number - 90000
        roman = roman + 'X' + macron + 'C' + macron

    if number >= 50000:
        number = number - 50000
        roman = roman + 'L' + macron

    if number >= 40000:
        number = number - 40000
        roman = roman + 'X' + macron + 'L' + macron

    while number >= 10000:
        number = number - 10000
        roman = roman + 'X' + macron

    if number >= 9000:
        number = number - 9000
        roman = roman + 'MX' + macron

    if number >= 5000:
        number = number - 5000
        roman = roman + 'V' + macron

    if number >= 4000:
        number = number - 4000
        roman = roman + 'MV' + macron

    while number >= 1000:
        number = number - 1000
        roman = roman + 'M'

    if number >= 900:
        number = number - 900
        roman = roman + 'CM'

    if number >= 500:
        number = number - 500
        roman = roman + 'D'

    if number >= 400:
        number = number - 400
        roman = roman + 'CD'

    while number >= 100:
        number = number - 100
        roman = roman + 'C'

    if number >= 90:
        number = number - 90
        roman = roman + 'XC'

    if number >= 50:
        number = number - 50
        roman = roman + 'L'

    if number >= 40:
        number = number - 40
        roman = roman + 'XL'

    while number >= 10:
        number = number - 10
        roman = roman + 'X'

    if number >= 9:
        number = number - 9
        roman = roman + 'IX'

    if number >= 5:
        number = number - 5
        roman = roman + 'V'

    if number >= 4:
        number = number - 4
        roman = roman + 'IV'

    while number >= 1:
        number = number - 1
        roman = roman + 'I'

    print(roman)


print(' Integers to Roman Numerals '.center(40, '*'))

# Handle value errors
try:
    user_input = int(input('Enter a number to convert to Roman numerals: '))
    integer_to_roman(user_input)
except ValueError:
    print('Invalid input. Enter an integer.')

Excute first approach on Replit.
Execute second approach on Replit.