
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert a Number to Roman Numerals in C#
Problem Description
In this problem, we are given a number, and we need to convert it to its equivalent Roman number representation using C#.
Example 1
- Input: number = 58
- Output: Roman numeral = LVIII
Explanation:
58 in Roman numerals is written as L (50) + V (5) + III (3).
Example 2
- Input: number = 1994
- Output: Roman numeral = MCMXCIV
Explanation:
1994 in Roman numerals is written as M (1000) + CM (900) + XC (90) + IV (4).
Below are different approaches for converting a number to Roman numerals:
Using an Iterative Approach
This is a straightforward approach to converting a number into Roman numerals. We use a predefined dictionary to map Roman numerals to their integer values in descending order. Then, we iteratively subtract the largest possible value from the given number while appending the corresponding Roman numeral to the result string.
Implementation Code
using System; using System.Collections.Generic; class Program { static string ConvertToRomanIterative(int number) { // Define Roman numeral mappings Dictionary < int, string > romanMap = new Dictionary < int, string > { {1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"} }; string result = ""; foreach(var item in romanMap) { while (number >= item.Key) { result += item.Value; // Append Roman numeral number -= item.Key; // Reduce the number } } return result; } static void Main() { int number = 1994; string romanNumeral = ConvertToRomanIterative(number); Console.WriteLine($"The Roman numeral for {number} is: {romanNumeral}"); } }
Output:
The Roman numeral for 1994 is: MCMXCIV
Time Complexity: O(n)
Space Complexity: O(1)
Using StringBuilder for Efficiency
In this approach, we use StringBuilder to optimize string concatenation, which can be expensive in iterative methods. The logic remains similar to the iterative approach, but StringBuilder makes it faster for larger inputs.
Implementation Code
using System; using System.Collections.Generic; using System.Text; class Program { static string ConvertToRomanUsingStringBuilder(int number) { // Define Roman numeral mappings Dictionary < int, string > romanMap = new Dictionary < int, string > { {1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"} }; StringBuilder result = new StringBuilder(); foreach(var item in romanMap) { while (number >= item.Key) { result.Append(item.Value); number -= item.Key; } } return result.ToString(); } static void Main() { int number = 58; string romanNumeral = ConvertToRomanUsingStringBuilder(number); Console.WriteLine($"The Roman numeral for {number} is: {romanNumeral}"); } }
Output:
The Roman numeral for 58 is: LVIII
Time Complexity: O(n)
Space Complexity: O(1)
Recursive Approach
We can use recursive approach to convert a number to roman number. In this approach, we recursively subtract the largest Roman numeral value from the given number until the number becomes zero. This method builds the result string step-by-step during the recursive calls.
Implementation Code
using System; using System.Collections.Generic; class Program { static string ConvertToRomanIterative(int number) { // Define Roman numeral mappings Dictionary < int, string > romanMap = new Dictionary < int, string > { {1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, {90, "XC"}, {50, "L"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"} }; string result = ""; foreach(var item in romanMap) { while (number >= item.Key) { result += item.Value; // Append Roman numeral number -= item.Key; // Reduce the number } } return result; } static void Main() { int number = 1994; string romanNumeral = ConvertToRomanIterative(number); Console.WriteLine($"The Roman numeral for {number} is: {romanNumeral}"); } }
Output:
The Roman numeral for 3999 is: MMMCMXCIX
Time Complexity: O(n)
Space Complexity: O(n)
Real-Life Applications of Roman Numbers
-
Education Tools:
Sometimes, it is needed to convert numbers to Roman numerals for learning purposes and is also required in education tools. -
Historical Representations:
Roman numbers are used in clocks, book chapters, and movie credits for aesthetic or traditional reasons. -
Game Development:
Roman numbers are used in scoreboards, levels, or ranks to give a classical touch.