Answer to Question #187120 in Python for mani

Question #187120

Roman Numerals


Write a program to convert a non-negative integer

N to its Roman numeral representation.Roman numerals are usually written largest to smallest from left to right.

symbol value

I 1

V 5

X 10

L 50

C 100

D 500

M 1000

A number containing several decimal digits is built by appending Roman numeral equivalent for each, from highest to lowest, as in the following examples:

  • 39 = XXX + IX = XXXIX.
  • 246 = CC + XL + VI = CCXLVI.
  • 789 = DCC + LXXX + IX = DCCLXXXIX.
  • 2,421 = MM + CD + XX + I = MMCDXXI.
  • 160 = C + LX = CLX
  • 207 = CC + VII = CCVII
  • 1,009 = M + IX = MIX
  • 1,066 = M + LX + VI = MLXVI
  • 1776 = M + DCC + LXX + VI = MDCCLXXVI
  • 1918 = M + CM + X + VIII = MCMXVIII

Input

The input will be a single line containing a positive integer N.

The input number not be greater than 104.


Output

The output should be a single line containing representation of Roman Numeral of number N.


See Roman Numerals Table for Symbol and corresponding value.




Sample Input 1

2


Sample Output 1

II


Sample Input 2

1994


Sample Output 2

MCMXCIV


1
Expert's answer
2021-04-30T09:54:11-0400
def Roman(number):
    numbers = {1000: 'M', 900: 'CM', 500: 'D', 400: 'CD', 100: 'C', 90: 'XC', 50: 'L', 40: 'XL', 10: 'X', 9: 'IX',
               5: 'V', 4: 'IV', 1: 'I'}
    rom = ""
    for key in numbers.keys():
        count = int(number / key)
        rom += numbers[key] * count
        number -= key * count
    return rom


n = int(input())
print(Roman(n))

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS