Roman numerals
Input
2
def roman_num(n):
th = n // 1000
res = 'M'*th
n %= 1000
if n >= 900:
res += 'CM'
n -= 900
if n >= 500:
res += 'D'
n -= 500
if n >= 400:
res += 'CD'
n -= 400
c = n // 100
res += 'C'*c
n %= 100
if n >= 90:
res += 'XC'
n -= 90
if n >= 50:
res += 'L'
n -= 50
if n >= 40:
res += 'XL'
n -= 40
d = n // 10
res += 'X'*d
n %= 10
if n == 9:
res += 'IX'
elif n >= 5:
res += 'V' + 'I'*(n-5)
elif n == 4:
res += 'IV'
else:
res += 'I'*n
return res
def main():
n = int(input())
s = roman_num(n)
print(s)
main()
Comments
Leave a comment