Skip to main content

Decimal to roman numeral converter in python

Python program for Decimal to roman numeral converter. Here mentioned other language solution.

#  Python 3 program for
#  Conversion from Decimal to roman number
class Perform :
    # Display roman value of n
    @staticmethod
    def result( n) :
        if (n==1):
            print("I", end ="")
        elif(n==4):
            print("IV", end ="")
        elif(n==5):
            print("V", end ="")
        elif(n==9):
            print("IX", end ="")
        elif(n==10):
            print("X", end ="")
        elif(n==40):
            print("XL", end ="")
        elif(n==50):
            print("L", end ="")
        elif(n==90):
            print("XC", end ="")
        elif(n==100):
            print("C", end ="")
        elif(n==400):
            print("CD", end ="")
        elif(n==500):
            print("D", end ="")
        elif(n==900):
            print("DM", end ="")
        elif(n==1000):
            print("M", end ="")
    @staticmethod
    def  select( number,  collection,  size) :
        n = 1
        i = 0
        i = 0
        while (i < size) :
            if (number >= collection[i]) :
                n = collection[i]
            else :
                break
            i += 1
        Perform.result(n)
        # Reduce the value of number
        return number - n
    @staticmethod
    def romanNo( number) :
        if (number <= 0) :
            # When is not a natural number
            return
        # Base case collection
        collection = [1, 4, 5, 9, 10, 40, 
        50, 90, 100, 400, 500, 900, 1000]
        # Get the size of collection
        size = len(collection)
        print(" " + str(number) , end =" : ")
        while (number > 0) :
            number = Perform.select(number, 
                collection, size)
        # Add new line
        print()

if __name__=="__main__":
    # Test Case
    Perform.romanNo(10)
    Perform.romanNo(18)
    Perform.romanNo(189)
    Perform.romanNo(604)
    Perform.romanNo(982)
    Perform.romanNo(3000)

Output

 10 : X
 18 : XVIII
 189 : CLXXXIX
 604 : DCIV
 982 : DMLXXXII
 3000 : MMM




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment