![]() |
VOOZH | about |
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.
Below example shows the implementation of the above algorithm:
Output:
Roman value is: MMMDXLIX
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:
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:
So, the output will be: MMMDXLIX
The below example shows the implementation of the above approach:
Output:
MMMDXLIX
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:
Output:
Roman value for the integer is: MMMDXLIX
Time Complexity: O(n)
Auxiliary Space: O(n)
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.
For this purpose we will use the toRoman() method of the roman package, it takes the integer value as it's argument.
Output -
MCMIV
Time Complexity - O(1)
Auxiliary Space - O(1)
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.