Wednesday, February 1, 2017

Roman Numeral of Given Input Number Python

Tags

Following Code will print the Equivalent Roman Numeral of Input Number :

Code::

def romandigit(n,a,b,c):
    if(n==0):return ""
    elif(n==1):return a
    elif(n==2):return a+a
    elif(n==3):return a+a+a
    elif(n==4):return a+b
    elif(n==5):return b
    elif(n==6):return b+a
    elif(n==7):return b+a+a
    elif(n==8):return b+a+a+a
    elif(n==9):return a+c
    
def romannumber(n):
    if(n>999):
        raise RuntimeError('Not Allowed')
    if(n<=99):
        return romandigit((n//10)%10,'X','L','C')+romandigit(n%10,'I','V','X')
    elif(n<=999):
        return romandigit((n//100)%10,'C','D','M')+romandigit((n//10)%10,'X','L','C')+romandigit(n%10,'I','V','X')

def main():
    i=int(input("Enter Any Number Upto 999:"))
    print(romannumber(i))

main()

Output::