Roman Numerals

Level: Beginner 15–30 min

Concepts: AlgorithmsNumbers


Create a Roman numeral converter that can convert between Roman numerals and Arabic numbers.

Requirements

  1. Convert Arabic numbers to Roman numerals (1 to 3999)
  2. Convert Roman numerals to Arabic numbers
  3. Handle invalid Roman numerals appropriately
  4. Follow standard Roman numeral rules:
    • I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, M = 1000
    • Letters can be repeated up to 3 times (e.g., III = 3)
    • When a smaller value precedes a larger value, subtract the smaller (e.g., IV = 4)
    • Only I, X, and C can be used as subtractive numerals

Test Cases

Arabic NumberRoman NumeralNotes
1IBasic conversion
4IVSubtractive notation
9IXSubtractive notation
40XLSubtractive notation
90XCSubtractive notation
400CDSubtractive notation
900CMSubtractive notation
1984MCMLXXXIVComplex number
3999MMMCMXCIXMaximum valid number

Invalid Cases to Handle

  • Numbers less than 1 or greater than 3999
  • Invalid Roman numeral characters
  • Invalid Roman numeral sequences (e.g., “IIII”, “VV”)
  • Invalid subtractive combinations (e.g., “IL”, “IC”)

Tips

  • Start with the simplest conversions first (1-10)
  • Add support for subtractive notation gradually
  • Consider edge cases and invalid inputs
  • Think about how to validate Roman numerals
  • Consider using a state machine for validation