Answer to Question #310282 in Python for Mock test 2a

Question #310282

Roman numerals




Input



2




1
Expert's answer
2022-03-12T12:46:15-0500
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()

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