Converting Integer to Roman Numerals

 

# integer to roman numerals


Num = int(input("Enter number between 1 and 3999 : "))

N=Num


if(Num<1 or Num>3999):

print("Invalid number.")

exit(0)


D={0:'',1:'I', 4:'IV', 5:'V', 9:'IX', 10:'X', 40:'XL', 50:'L', 90:'XC', 100:'C', 400:'CD', 500:'D', 900:'CM', 1000:'M'}


res=''


div=1

while(N>=div):

base=5*div

r = N%(div*10)

N = N-r


tmp=''

if r not in D.keys():

if(r<base):

tmp=D[div]*(r//div)

else:

r=r-base

tmp=D[base]+D[div]*(r//div)

else:

tmp=D[r]


div=div*10

res=tmp+res

print(Num,":",res)


No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu