Open In App

Converting Decimal Number lying between 1 to 3999 to Roman Numerals

Last Updated : 30 Apr, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Given an integer, convert it into its equivalent Roman numeral representation.

Note: Following is the list of Roman symbols (including subtractive cases):

SymbolValue
I1
IV4
V5
IX9
X10
XL40
L50
XC90
C100
CD400
D500
CM900
M1000

Examples: 

Input: 9
Output: IX
Explanation: 9 is written as "IX" in Roman numerals using subtractive notation — placing a smaller numeral before a larger one.

  • I = 1, X = 10
  • IX means 10 - 1 = 9

Input: 40
Output: XL
Explanation: 40 is written as "XL" in Roman numerals using subtractive notation — placing a smaller numeral before a larger one.

  • X = 10, L = 50
  • XL means 50 - 10 = 40

[General Purpose Solution] - O(n) Time and O(n) Space

Compare given number with base values in the order 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1. Once we find the largest base value that is less than the given number, we divide the number with the base value and repeat the process for smaller base values and quotient. We add the roman symbol corresponding to the base value found to the result, number of times equal to the quotient and repeat the process for remainder.

Let us understand the approach with an example 3549

Iteration 1

  • Since 3549 >= 1000 ; largest base value will be 1000 initially.
  • Divide 3549/1000. Quotient = 3, res = "MMM" (Note M belongs to 1000)
  • Remainder = 549

Iteration 2

  • 1000 > 549 >= 500 ; largest base value will be 500.
  • Divide 549/500. Quotient = 1, .res = "MMMD"
  • Remainder = 49

Iteration 3

  • 50 > 49 >= 40 ; largest base value is 40.
  • Divide 49/40. Quotient = 1, res = "MMMDXL"
  • Remainder = 9.

Iteration 4

  • Number 9 is present in list. res = "MMMDXL"
  • Remainder = 0.
C++
#include <bits/stdc++.h>
using namespace std;

// Function to convert decimal to Roman Numerals
string toRoman(int x) {

    // array of values and symbols
    vector<int> base = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
    vector<string> sym = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};

    // to store result
    string res = "";

    // Loop from the right side to find
    // the largest smaller base value
    int i = base.size() - 1;
    while (x > 0) {
        int div = x / base[i];
        while (div) {
            res += sym[i];
            div--;
        }
        
        // Repeat the process for remainder
        x = x % base[i];
        
        i--;
    }

    return res;
}

int main() {
    int x = 3549;
    cout << toRoman(x);
    return 0;
}
Java
// Function to convert decimal to Roman Numerals
public class RomanConverter {
    public static String toRoman(int x) {
        
        // array of values and symbols
        int[] base = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
        String[] sym = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};

        // to store result
        StringBuilder res = new StringBuilder();

        // Loop from the right side to find
        // the largest smaller base value
        int i = base.length - 1;
        while (x > 0) {
            int div = x / base[i];
            while (div > 0) {
                res.append(sym[i]);
                div--;
            }
            
            // Repeat the process for remainder
            x = x % base[i];
            i--;
        }

        return res.toString();
    }

    public static void main(String[] args) {
        int x = 3549;
        System.out.println(toRoman(x));
    }
}
Python
# Function to convert decimal to Roman Numerals
def to_roman(x):
    
    # array of values and symbols
    base = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000]
    sym = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"]

    # to store result
    res = ""

    # Loop from the right side to find
    # the largest smaller base value
    i = len(base) - 1
    while x > 0:
        div = x // base[i]
        while div:
            res += sym[i]
            div -= 1
        # Repeat the process for remainder
        x %= base[i]
        i -= 1

    return res

x = 3549
print(to_roman(x))
C#
// Function to convert decimal to Roman Numerals
public class RomanConverter {
    public static string ToRoman(int x) {
        
        // array of values and symbols
        int[] baseValues = {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
        string[] symbols = {"I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"};

        // to store result
        string res = "";

        // Loop from the right side to find
        // the largest smaller base value
        int i = baseValues.Length - 1;
        while (x > 0) {
            int div = x / baseValues[i];
            while (div > 0) {
                res += symbols[i];
                div--;
            }
            
            // Repeat the process for remainder
            x %= baseValues[i];
            i--;
        }

        return res;
    }

    public static void Main() {
        int x = 3549;
        Console.WriteLine(ToRoman(x));
    }
}
JavaScript
// Function to convert decimal to Roman Numerals
function toRoman(x) {
    
    // array of values and symbols
    const base = [1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000];
    const sym = ["I", "IV", "V", "IX", "X", "XL", "L", "XC", "C", "CD", "D", "CM", "M"];

    // to store result
    let res = "";

    // Loop from the right side to find
    // the largest smaller base value
    let i = base.length - 1;
    while (x > 0) {
        let div = Math.floor(x / base[i]);
        while (div) {
            res += sym[i];
            div--;
        }
        
        // Repeat the process for remainder
        x %= base[i];
        i--;
    }

    return res;
}

let x = 3549;
console.log(toRoman(x));

Output
MMMDXLIX

Time Complexity: O(n), where n is the length of the answer string that stores the conversion.
Auxiliary Space: O(n)

[For Limited Range] - O(n) Time and O(n) Space

The idea is based on the fact that we have a limited range to covert 0 to 3999. We isolate the digits corresponding to the thousands, hundreds, tens, and ones places, and then mapping each digit to its respective Roman numeral equivalent based on its positional value.

  • Store mappings of character M for different quotients, 0, 1, 2, 3
  • Store mappings of C, L and I for different quotients form 0 to 9.

Using the above mappings, we directly generate the result string.

C++
#include <bits/stdc++.h>
using namespace std;

// Function to convert decimal to Roman Numerals
string toRoman(int val) { 

    // storing roman values of digits from 0-9
    // when placed at different places
    vector<string> m = {"", "M", "MM", "MMM"};
    vector<string> c = {"", "C", "CC", "CCC", "CD",
                        "D", "DC", "DCC", "DCCC", "CM"};
    vector<string> x = {"", "X", "XX", "XXX", "XL",
                        "L", "LX", "LXX", "LXXX", "XC"};
    vector<string> i = {"", "I", "II", "III", "IV",
                        "V", "VI", "VII", "VIII", "IX"};

    // Converting to roman
    string thousands = m[val / 1000];
    string hundreds = c[(val % 1000) / 100];
    string tens = x[(val % 100) / 10];
    string ones = i[val % 10];

    string ans = thousands + hundreds + tens + ones;

    return ans;
}

int main() {
    int val = 3549;
    cout << toRoman(val);
    return 0;
}
Java
import java.util.*;

public class GfG {

    // Function to convert decimal to Roman Numerals
    public static String toRoman(int val) { 

        // storing roman values of digits from 0-9
        // when placed at different places
        String[] m = {"", "M", "MM", "MMM"};
        String[] c = {"", "C", "CC", "CCC", "CD",
                        "D", "DC", "DCC", "DCCC", "CM"};
        String[] x = {"", "X", "XX", "XXX", "XL",
                        "L", "LX", "LXX", "LXXX", "XC"};
        String[] i = {"", "I", "II", "III", "IV",
                        "V", "VI", "VII", "VIII", "IX"};

        // Converting to roman
        String thousands = m[val / 1000];
        String hundreds = c[(val % 1000) / 100];
        String tens = x[(val % 100) / 10];
        String ones = i[val % 10];

        String ans = thousands + hundreds + tens + ones;

        return ans;
    }

    public static void main(String[] args) {
        int val = 3549;
        System.out.println(toRoman(val));
    }
}
Python
# Function to convert decimal to Roman Numerals
def toRoman(val): 

    # storing roman values of digits from 0-9
    # when placed at different places
    m = ["", "M", "MM", "MMM"]
    c = ["", "C", "CC", "CCC", "CD",
         "D", "DC", "DCC", "DCCC", "CM"]
    x = ["", "X", "XX", "XXX", "XL",
         "L", "LX", "LXX", "LXXX", "XC"]
    i = ["", "I", "II", "III", "IV",
         "V", "VI", "VII", "VIII", "IX"]

    # Converting to roman
    thousands = m[val // 1000]
    hundreds = c[(val % 1000) // 100]
    tens = x[(val % 100) // 10]
    ones = i[val % 10]

    ans = thousands + hundreds + tens + ones

    return ans

if __name__ == "__main__":
    val = 3549
    print(toRoman(val))
C#
using System;

public class GfG {

    // Function to convert decimal to Roman Numerals
    public static string toRoman(int val) { 

        // storing roman values of digits from 0-9
        // when placed at different places
        string[] m = {"", "M", "MM", "MMM"};
        string[] c = {"", "C", "CC", "CCC", "CD",
                        "D", "DC", "DCC", "DCCC", "CM"};
        string[] x = {"", "X", "XX", "XXX", "XL",
                        "L", "LX", "LXX", "LXXX", "XC"};
        string[] i = {"", "I", "II", "III", "IV",
                        "V", "VI", "VII", "VIII", "IX"};

        // Converting to roman
        string thousands = m[val / 1000];
        string hundreds = c[(val % 1000) / 100];
        string tens = x[(val % 100) / 10];
        string ones = i[val % 10];

        string ans = thousands + hundreds + tens + ones;

        return ans;
    }

    public static void Main(string[] args) {
        int val = 3549;
        Console.WriteLine(toRoman(val));
    }
}
JavaScript
// Function to convert decimal to Roman Numerals
function toRoman(val) { 

    // storing roman values of digits from 0-9
    // when placed at different places
    let m = ["", "M", "MM", "MMM"];
    let c = ["", "C", "CC", "CCC", "CD",
             "D", "DC", "DCC", "DCCC", "CM"];
    let x = ["", "X", "XX", "XXX", "XL",
             "L", "LX", "LXX", "LXXX", "XC"];
    let i = ["", "I", "II", "III", "IV",
             "V", "VI", "VII", "VIII", "IX"];

    // Converting to roman
    let thousands = m[Math.floor(val / 1000)];
    let hundreds = c[Math.floor((val % 1000) / 100)];
    let tens = x[Math.floor((val % 100) / 10)];
    let ones = i[val % 10];

    let ans = thousands + hundreds + tens + ones;

    return ans;
}

let val = 3549;
console.log(toRoman(val));

Output
MMMDXLIX

Next Article
Article Tags :
Practice Tags :

Similar Reads