In this article, we are going to create a program that converts integer values to roman values. Roman value is a roman system of representing the number
Example:-
1=” I ”, 2=” II ”, 3=” III ”
The Roman system does not have negative numbers and zero is defined by the word “nulla”
ALGORITHM:
- Defining the function
- Creating the catalog from which the value will determine
- Operating and finding the roman value
- Running the drivers code
Source code to convert an integer into roman:-
# INTEGER to ROMAN def roman(num): integer=[1,4,5,9,10,40,50,90,100,400,500,900,1000] symbols = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"] i=12 if num==0: print("nulla") elif num<0: print("negative numbers are not allowed in the Roman system") else: while num: a=num//integer[i] num%=integer[i] while a: print(symbols[i],end="") a-=1 i-=1 if __name__ == '__main__': value = int(input("enter the value: ")) roman(value)
Explanation of the code:-
- Creating function with a parameter that will hold the value of the number given by the user
- Defining the numbers and symbols in a list so that they can be called whenever they are needed
- As there is no place of any negative number and zero so using the conditional statements to filter the input of the user
- Then accessing the digit one by one of number and then converting that number into roman according to need
- In the end, we are printing it using a while loop
execution of the code:-
entered value: 49
output: XLIX