You are on page 1of 3

Roman numerals

Python code
• print("Correct for 1 - 3999")

• def romannum(number):

• thousand = number // 1000
• number = number - thousand * 1000
• hund = number // 100
• number = number - hund * 100
• ten = number // 10
• one = number - ten * 10

• romthou = "M" * thousand

• if hund in [0,1,2,3]:
• romhund = hund * "C"
• elif hund == 4:
• romhund = "CD"
• elif hund == 9:
• romhund = "CM"
• else:
• romhund = "D" + (hund - 5) * "C"
• if ten in [0,1,2,3]:
• romten = ten * "X"
• elif ten == 4:
• romten = "XL"
• elif ten == 9:
• romten = "XC"
• else:
• romten = "L" + "X" * (ten - 5)

• if one in [0,1,2,3]:
• romone = one * "I"
• elif one == 4:
• romone = "IV"
• elif one == 9:
• romone = "IX"
• else:
• romone = "V" + "I" * (one - 5)

• return romthou + romhund + romten + romone

You might also like