Welcome to this comprehensive guide on how to create a Python program to convert an integer to a Roman numeral.
In this tutorial, we will explore various approaches to solve this problem step by step.
By the end, you will have a solid understanding of how to tackle this task in Python.
Intro
Introduction to Roman Numerals
Before we dive into the code, let’s have a brief overview of Roman numerals.
Roman numerals are a numeral system that originated in ancient Rome.
They are composed of several symbols that represent different values.
The basic Roman numerals and their corresponding values are as follows:
- I: 1
- V: 5
- X: 10
- L: 50
- C: 100
- D: 500
- M: 1000
Roman numerals are formed by combining these symbols in various ways.
For example, the number 3 is represented as ‘III’, 9 as ‘IX’, and 40 as ‘XL’.
Approach 1
Direct Conversion Using a Dictionary
In this approach, we will create a dictionary that maps integers to their corresponding Roman numeral symbols.
We will then iterate through the dictionary and subtract the largest possible value from the integer until it reaches 0.
Python Program to Convert Integer to Roman
# Python program to convert integer to Roman numeral
def int_to_roman(num):
# Define a dictionary to store the mapping of integers to Roman numerals
roman_map = {
1000: 'M', # 'M' represents 1000 in Roman numerals
900: 'CM', # 'CM' represents 900 in Roman numerals
500: 'D', # 'D' represents 500 in Roman numerals
400: 'CD', # 'CD' represents 400 in Roman numerals
100: 'C', # 'C' represents 100 in Roman numerals
90: 'XC', # 'XC' represents 90 in Roman numerals
50: 'L', # 'L' represents 50 in Roman numerals
40: 'XL', # 'XL' represents 40 in Roman numerals
10: 'X', # 'X' represents 10 in Roman numerals
9: 'IX', # 'IX' represents 9 in Roman numerals
5: 'V', # 'V' represents 5 in Roman numerals
4: 'IV', # 'IV' represents 4 in Roman numerals
1: 'I' # 'I' represents 1 in Roman numerals
}
roman_numeral = '' # Initialize an empty string to store the Roman numeral representation
for value, symbol in roman_map.items(): # Iterate through the dictionary items
while num >= value: # While the input number is greater than or equal to the current value
roman_numeral += symbol # Append the corresponding Roman numeral symbol to the result
num -= value # Subtract the value from the input number
return roman_numeral # Return the Roman numeral representation
# Test the function
num = 354 # Define the input number
print(f"The Roman numeral representation of {num} is: {int_to_roman(num)}") # Print the result
Output
The Roman numeral representation of 354 is: CCCLIV
Explanation: Python Program to Convert Integer to Roman
This code defines a function int_to_roman() that converts an integer to its Roman numeral representation using a dictionary-based approach.
- We named a dictionary roman_map to map integers to their corresponding Roman numeral symbols.
- The function iterates through the items in the roman_map dictionary. It checks if the input number num is greater than or equal to the current integer value from the dictionary. If it is, the corresponding Roman numeral symbol is appended to the roman_numeral string, and the value is subtracted from
num
. - The function returns the Roman numeral representation stored in the roman_numeral string.
- The code then tests the int_to_roman() function by passing the integer 354 as input and prints the resulting Roman numeral representation to the console.
Approach 2
Recursive Approach
Another approach to convert an integer to a Roman numeral is by using recursion.
In this approach, we recursively subtract the largest possible value from the integer and append the corresponding Roman numeral symbol.
Python Program to Convert Integer to Roman
# Python program to convert integer to Roman numeral using recursion
def int_to_roman_recursive(num):
# Define a dictionary to store the mapping of integers to Roman numerals
roman_map = {
1000: 'M', # 'M' represents 1000 in Roman numerals
900: 'CM', # 'CM' represents 900 in Roman numerals
500: 'D', # 'D' represents 500 in Roman numerals
400: 'CD', # 'CD' represents 400 in Roman numerals
100: 'C', # 'C' represents 100 in Roman numerals
90: 'XC', # 'XC' represents 90 in Roman numerals
50: 'L', # 'L' represents 50 in Roman numerals
40: 'XL', # 'XL' represents 40 in Roman numerals
10: 'X', # 'X' represents 10 in Roman numerals
9: 'IX', # 'IX' represents 9 in Roman numerals
5: 'V', # 'V' represents 5 in Roman numerals
4: 'IV', # 'IV' represents 4 in Roman numerals
1: 'I' # 'I' represents 1 in Roman numerals
}
if num == 0: # Base case: If the input number is 0, return an empty string
return ''
for value, symbol in roman_map.items(): # Iterate through the dictionary items
if num >= value: # If the input number is greater than or equal to the current value
return symbol + int_to_roman_recursive(num - value) # Return the symbol concatenated with the recursive call
return '' # Return an empty string if the input number is not greater than or equal to any value
# Test the function
num = 13 # Define the input number
print(f"The Roman numeral representation of {num} is: {int_to_roman_recursive(num)}") # Print the result
Output
The Roman numeral representation of 13 is: XIII
Explanation: Python Program to Convert Integer to Roman
This code defines a function int_to_roman_recursive() that converts an integer to its Roman numeral representation using a recursive approach.
- A dictionary named roman_map is defined to map integers to their corresponding Roman numeral symbols.
- The function checks for the base case: if the input number num is 0, it returns an empty string, signifying the end of the recursion.
- The function iterates through the items in the roman_map dictionary. If the input number is greater than or equal to the current integer value, it returns the concatenation of the current symbol and a recursive call with the updated input number (num - value).
- The code then tests the int_to_roman_recursive() function by passing the integer 13 as input and prints the resulting Roman numeral representation to the console.
In summary, the code recursively builds the Roman numeral representation by subtracting the largest possible values from the input number until it reaches 0.
The result is obtained by concatenating the corresponding Roman numeral symbols.
Approach 3
Approach 3: Using a Loop with Divmod
Another approach to convert an integer to a Roman numeral is by using a loop with the divmod() function.
The divmod() function returns the quotient and remainder of the division operation.
Python Program to Convert Integer to Roman
# Python program to convert integer to Roman numeral using divmod
def int_to_roman_divmod(num):
# Define a dictionary to store the mapping of integers to Roman numerals
roman_map = {
1000: 'M', # 'M' represents 1000 in Roman numerals
900: 'CM', # 'CM' represents 900 in Roman numerals
500: 'D', # 'D' represents 500 in Roman numerals
400: 'CD', # 'CD' represents 400 in Roman numerals
100: 'C', # 'C' represents 100 in Roman numerals
90: 'XC', # 'XC' represents 90 in Roman numerals
50: 'L', # 'L' represents 50 in Roman numerals
40: 'XL', # 'XL' represents 40 in Roman numerals
10: 'X', # 'X' represents 10 in Roman numerals
9: 'IX', # 'IX' represents 9 in Roman numerals
5: 'V', # 'V' represents 5 in Roman numerals
4: 'IV', # 'IV' represents 4 in Roman numerals
1: 'I' # 'I' represents 1 in Roman numerals
}
roman_numeral = '' # Initialize an empty string to store the Roman numeral representation
for value in roman_map: # Iterate through the values in the dictionary
count, num = divmod(num, value) # Use divmod to get the quotient and remainder of num divided by the current value
roman_numeral += roman_map[value] * count # Append the corresponding Roman numeral symbol to the result, repeated 'count' times
return roman_numeral # Return the Roman numeral representation
# Test the function
num = 78 # Define the input number
print(f"The Roman numeral representation of {num} is: {int_to_roman_divmod(num)}") # Print the result
Output
The Roman numeral representation of 78 is: LXXVIII
Explanation:
This code defines a function int_to_roman_divmod() that converts an integer to its Roman numeral representation using the divmod() function.
- We named a dictionary roman_map to map integers to their corresponding Roman numeral symbols.
- The function initializes an empty string roman_numeral to store the resulting Roman numeral representation.
- The function iterates through the values in the roman_map dictionary. For each value, it uses the divmod() function to get the quotient (count) and remainder (num) when dividing the input number num by the current value.
- Then, we appended the corresponding Roman numeral symbol to the roman_numeral string, repeated ‘count’ times.
- The function returns the final Roman numeral representation.
- The code tests the int_to_roman_divmod() function by passing the integer 78 as input and prints the resulting Roman numeral representation to the console.
Approach 4
Using a List of Tuples
In this approach, we use a list of tuples to store the mapping of integers to Roman numerals.
We iterate through the list and subtract the largest possible value from the integer until it reaches 0.
Python Program to Convert Integer to Roman
# Python program to convert integer to Roman numeral using a list of tuples
def int_to_roman_tuples(num):
# Define a list of tuples to store the mapping of integers to Roman numerals
roman_map = [
(1000, 'M'), # Tuple representing 1000 as 'M' in Roman numerals
(900, 'CM'), # Tuple representing 900 as 'CM' in Roman numerals
(500, 'D'), # Tuple representing 500 as 'D' in Roman numerals
(400, 'CD'), # Tuple representing 400 as 'CD' in Roman numerals
(100, 'C'), # Tuple representing 100 as 'C' in Roman numerals
(90, 'XC'), # Tuple representing 90 as 'XC' in Roman numerals
(50, 'L'), # Tuple representing 50 as 'L' in Roman numerals
(40, 'XL'), # Tuple representing 40 as 'XL' in Roman numerals
(10, 'X'), # Tuple representing 10 as 'X' in Roman numerals
(9, 'IX'), # Tuple representing 9 as 'IX' in Roman numerals
(5, 'V'), # Tuple representing 5 as 'V' in Roman numerals
(4, 'IV'), # Tuple representing 4 as 'IV' in Roman numerals
(1, 'I') # Tuple representing 1 as 'I' in Roman numerals
]
roman_numeral = '' # Initialize an empty string to store the Roman numeral representation
for value, symbol in roman_map: # Iterate through the tuples in the list
while num >= value: # While the input number is greater than or equal to the current value
roman_numeral += symbol # Append the corresponding Roman numeral symbol to the result
num -= value # Subtract the value from the input number
return roman_numeral # Return the Roman numeral representation
# Test the function
num = 112 # Define the input number
print(f"The Roman numeral representation of {num} is: {int_to_roman_tuples(num)}") # Print the result
Output
The Roman numeral representation of 112 is: CXII
Explanation:
This code defines a function int_to_roman_tuples() that converts an integer to its Roman numeral representation using a list of tuples.
- We named a list of tuples roman_map to map integers to their corresponding Roman numeral symbols. Each tuple contains an integer value and its corresponding Roman numeral symbol.
- The function initializes an empty string roman_numeral to store the resulting Roman numeral representation.
- The function iterates through the tuples in the roman_map list. For each tuple, it checks if the input number num is greater than or equal to the current integer value. If it is, the corresponding Roman numeral symbol is appended to the roman_numeral string, and the value is subtracted from num.
- The function returns the final Roman numeral representation.
- The code tests the int_to_roman_tuples() function by passing the integer 112 as input and prints the resulting Roman numeral representation to the console.
In summary, the code uses a list of tuples to map integers to their corresponding Roman numeral symbols and iteratively constructs the Roman numeral representation by subtracting the appropriate values from the input integer.
Wrapping Up
Conclusions: Python Program to Convert Integer to Roman
Congratulations! You have learned different approaches to create a Python program that converts an integer to a Roman numeral.
We explored various techniques, including direct conversion using a dictionary, recursion, using a loop with divmod, and using a list of tuples.
Each approach has its advantages.
And your can use each approach based on the requirements of your project.
Feel free to experiment with the code and try implementing additional features or optimizations.
If you have any questions, suggestions, or feedback, please leave a comment below.
Access our full programs library here.
Happy coding🐍!
Discover more from Python Mania
Subscribe to get the latest posts sent to your email.