Convert a positive integer between 1 and 3999 to its Roman numeral representation. Roman numerals use seven symbols: I = 1, V = 5, X = 10, L = 50, C = 100, D = 500, and M = 1000. Subtractive notation is used for the values 4, 9, 40, 90, 400, and 900, represented as IV, IX, XL, XC, CD, and CM respectively.

To form a Roman numeral, repeatedly subtract the largest possible value and append the corresponding symbol until the number reaches zero. This greedy strategy produces the standard Roman numeral form.

Examples:

Input: num = 3 Output: 'III' Explanation: Three ones. Input: num = 4 Output: 'IV' Explanation: Subtractive notation: I before V means 4. Input: num = 9 Output: 'IX' Explanation: Subtractive notation: I before X means 9. Input: num = 58 Output: 'LVIII' Explanation: 50 + 5 + 3 gives L + V + III. Input: num = 1994 Output: 'MCMXCIV' Explanation: 1000 + 900 + 90 + 4 gives M + CM + XC + IV.
Apex Code Editor

Welcome to Lightning Challenge!

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay July 2026

Contest runs July 1 - 31. Complete challenges to climb the leaderboard!

Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In