Python program to convert roman numbers into integer values using dictionaries.

Write a program to convert roman numbers into integer values using dictionaries.

Program:-

def roman_to_integer(roman_numeral):
    roman_to_int = {'I': 1, 'V': 5, 'X': 10, 'L': 50, 'C': 100, 'D': 500, 'M': 1000}
    total = 0
    prev_value = 0

    for char in reversed(roman_numeral):
        current_value = roman_to_int[char]

        if current_value >= prev_value:
            total += current_value
        else:
            total -= current_value

        prev_value = current_value

    return total

# Example usage
roman_numeral = input("Enter a Roman numeral: ")
result = roman_to_integer(roman_numeral)
print("Integer value:", result)

Output:-

Enter a Roman Number : XVII
17


Enter a Roman Number : MLXV

1066

Leave a Reply

Your email address will not be published. Required fields are marked *