Python program to convert integer to roman
Last Updated :
25 Apr, 2023
Given an integer, the task is to write a Python program to convert integer to roman.
Examples:
Input: 5
Output: V
Input: 9
Output: IX
Input: 40
Output: XL
Input: 1904
Output: MCMIV
Below table shows the list of Roman symbols including their corresponding integer values also:
Symbols | Values |
---|
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 |
Idea is to convert the units, tens, hundreds, and thousands of places of the given number separately. If the digit is 0, then there’s no corresponding Roman numeral symbol. The conversion of digits 4’s and 9’s are a little bit different from other digits because these digits follow subtractive notation.
Algorithm to convert an Integer value to Roman Numeral
Compare given number with base values in the order 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1. The base value that is just smaller or equal to the given number will be the initial base value (largest base value), Divide the number by its largest base value, the corresponding base symbol will be repeated quotient times, the remainder will then become the number for future division and repetitions. The process will be repeated until the number becomes zero.
Method 1:
- Initially number = 3549, Since 3549 >= 1000 ; largest base value will be 1000 initially. And Divide 3549/1000. Quotient = 3, Remainder =549. The corresponding symbol M will be repeated thrice.
- Now, number become 549 and 1000 > 549 >= 500, largest base value will be 500 then divide 549/500. Quotient = 1, Remainder =49. The corresponding symbol D will be repeated once.
- Now, number = 49 and 50 > 49 >= 40, largest base value is 40. Then divide 49/40. Quotient = 1, Remainder = 9. The corresponding symbol XL will be repeated once.
- Now, number = 9 and 10> 9 >= 9, largest base value is 9. Then divide 9/9. Quotient = 1, Remainder = 0. The corresponding symbol IX will be repeated once.
- Finally, the number becomes 0, the algorithm stops here. The output obtained MMMDXLIX.
Below example shows the implementation of the above algorithm:
Python3
# Python3 program to convert
# integer value to roman values
# Function to convert integer to Roman values
def printRoman(number):
num = [1, 4, 5, 9, 10, 40, 50, 90,
100, 400, 500, 900, 1000]
sym = ["I", "IV", "V", "IX", "X", "XL",
"L", "XC", "C", "CD", "D", "CM", "M"]
i = 12
while number:
div = number // num[i]
number %= num[i]
while div:
print(sym[i], end = "")
div -= 1
i -= 1
# Driver code
if __name__ == "__main__":
number = 3549
print("Roman value is:", end = " ")
printRoman(number)
Output:
Roman value is: MMMDXLIX
Method 2:
In this method, we have to first observe the problem. The number given in the problem statement can be a maximum of 4 digits. The idea to solve this problem is:
- Divide the given number into digits at different places like one’s, two’s, hundred’s, or thousand’s.
- Starting from the thousand’s place print the corresponding roman value. For example, if the digit at thousand’s place is 3 then print the roman equivalent of 3000.
- Repeat the second step until we reach one’s place.
Suppose the input number is 3549. So, starting from thousand’s place we will start printing the roman equivalent. In this case, we will print in the order as given below:
- The Roman equivalent of 3000
- The Roman equivalent of 500
- The Roman equivalent of 40
- The Roman equivalent of 9
So, the output will be: MMMDXLIX
The below example shows the implementation of the above approach:
Python3
# Python3 program for above approach
# Function to calculate Roman values
def intToRoman(num):
# Storing roman values of digits from 0-9
# when placed at different places
m = ["", "M", "MM", "MMM"]
c = ["", "C", "CC", "CCC", "CD", "D",
"DC", "DCC", "DCCC", "CM "]
x = ["", "X", "XX", "XXX", "XL", "L",
"LX", "LXX", "LXXX", "XC"]
i = ["", "I", "II", "III", "IV", "V",
"VI", "VII", "VIII", "IX"]
# Converting to roman
thousands = m[num // 1000]
hundreds = c[(num % 1000) // 100]
tens = x[(num % 100) // 10]
ones = i[num % 10]
ans = (thousands + hundreds +
tens + ones)
return ans
# Driver code
if __name__ == "__main__":
number = 3549
print(intToRoman(number))
Output:
MMMDXLIX
Method 3:
In this approach, we consider the main significant digit in the number. Ex: in 1234, the main significant digit is 1. Similarly, in 345 it is 3. In order to extract main significant digit out, we need to maintain a divisor (lets call it div) like 1000 for 1234 (since 1234 / 1000 = 1) and 100 for 345 (345 / 100 = 3). Also, let's maintain a dictionary called roman numeral = {1 : ‘I’, 5: ‘V’, 10: ‘X’, 50: ‘L’, 100: ‘C’, 500: ‘D’, 1000: ‘M’}
The below example shows the implementation of the above algorithm:
Python3
# Python 3 program to convert integer
# number to Roman values
import math
def integerToRoman(A):
romansDict = \
{
1: "I",
5: "V",
10: "X",
50: "L",
100: "C",
500: "D",
1000: "M",
5000: "G",
10000: "H"
}
div = 1
while A >= div:
div *= 10
div /= 10
res = ""
while A:
# main significant digit extracted
# into lastNum
lastNum = int(A / div)
if lastNum <= 3:
res += (romansDict[div] * lastNum)
elif lastNum == 4:
res += (romansDict[div] +
romansDict[div * 5])
elif 5 <= lastNum <= 8:
res += (romansDict[div * 5] +
(romansDict[div] * (lastNum - 5)))
elif lastNum == 9:
res += (romansDict[div] +
romansDict[div * 10])
A = math.floor(A % div)
div /= 10
return res
# Driver code
print("Roman value for the integer is:"
+ str(integerToRoman(3549)))
Output:
Roman value for the integer is: MMMDXLIX
Time Complexity: O(n)
Auxiliary Space: O(n)
Method - 4 -
In this approach we will use an external module called roman to convert an integer to roman and vice versa. We need to install it using pip command first. Write the below command in terminal.
!pip install roman
Then we will use two methods , one to convert integer into roman another to convert roman into integer.
Converting Integer to Roman -
For this purpose we will use the toRoman() method of the roman package, it takes the integer value as it's argument.
Python3
# Importing the package
import roman
# converting the integer to Roman
# and storing it in variable 'r'
r = roman.toRoman(1904)
# Printing the converted value
print(r)
Output -
MCMIV
Time Complexity - O(1)
Auxiliary Space - O(1)
Converting Roman to Integer -
Here we will convert a roman value to an integer value using fromRoman() method which takes the roman value as an argument, we need to pass that as a string.
Python3
# Importing the module roman
import roman
# Converting the roman value
# into integer and storing
# it in variable 'i'
i = roman.fromRoman("MCMIV")
# Printing the converted value
print(i)
Output -
1904
Time Complexity - O(1)
Auxiliary Space - O(1)
Similar Reads
Python program to convert hex string to decimal
Converting a hexadecimal string to a decimal number is a common task in programming, especially when working with binary data or low-level computations. Hexadecimal values offer a concise way to represent large numbers.Using the int() FunctionUsing the int() function is the most common and straightf
2 min read
Python program to convert a byte string to a list of integers
We have to convert a byte string to a list of integers extracts the byte values (ASCII codes) from the byte string and stores them as integers in a list. For Example, we are having a byte string s=b"Hello" we need to write a program to convert this string to list of integers so the output should be
2 min read
Python program to convert int to exponential
Given a number of int type, the task is to write a Python program to convert it to exponential. Examples: Input: 19 Output: 1.900000e+01 Input: 2002 Output: 2.002000e+03 Input: 110102 Output: 1.101020e+05Approach: We will first declare and initialise an integer numberThen we will use format method t
1 min read
Python Program to Convert Decimal to Hexadecimal
In this article, we will learn how to convert a decimal value(base 10) to a hexadecimal value (base 16) in Python. Method 1: Using hex() function hex() function is one of the built-in functions in Python3, which is used to convert an integer number into its corresponding hexadecimal form. Syntax :
3 min read
Python Program to Convert Binary to Hexadecimal
Given a binary number, the task is to write a Python program to convert the given binary number into an equivalent hexadecimal number. i.e convert the number with base value 2 to base value 16. In hexadecimal representation we 16 values to represent a number. Numbers 0-9 are expressed by digits 0-9
4 min read
Python program to convert exponential to float
Given a number in exponential format, the task is to write a Python program to convert the number from exponential format to float. The exponential number is a way of representing a number. Examples: Input: 1.900000e+01 Output: 19.0 Input: 2.002000e+03 Output: 2002.0 Input: 1.101020e+05 Output: 1101
1 min read
Python - Convert Integer Matrix to String Matrix
Given a matrix with integer values, convert each element to String. Input : test_list = [[4, 5, 7], [10, 8, 3], [19, 4, 6]] Output : [['4', '5', '7'], ['10', '8', '3'], ['19', '4', '6']] Explanation : All elements of Matrix converted to Strings. Input : test_list = [[4, 5, 7], [10, 8, 3]] Output : [
6 min read
Python program to convert float to exponential
Given a float number, the task is to write a Python program to convert float to exponential. Examples: Input: 19.0 Output: 1.900000e+01 Input: 200.2 Output: 2.002000e+02 Input: 1101.02 Output: 1.101020e+03Approach: We will first declare and initialise a float number.Then we will use format method to
1 min read
Python program to convert Base 4 system to binary number
Given a base 4 number N, the task is to write a python program to print its binary equivalent. Conversion Table: Examples: Input : N=11002233 Output : 101000010101111 Explanation : From that conversion table we changed 1 to 01, 2 to 10 ,3 to 11 ,0 to 00.Input : N=321321 Output: 111001111001Method 1:
3 min read
Python program to concatenate two Integer values into one
Given two integers a and b. The task is to concatenate these two integers into one integer. Examples: Input : a = 806, b = 91 Output : 80691 Input : a = 5, b = 1091 Output : 51091 Method 1: One method of achieving this can be counting the number of digits of second number. Then multiply the first nu
2 min read