Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Convert a number to Roman numerals in C#
Converting a number to Roman numerals in C# is a common programming problem that involves mapping integer values to their corresponding Roman numeral representations. Roman numerals use specific symbols like I, V, X, L, C, D, and M to represent numbers.
Roman Numeral System
Roman numerals follow specific rules where certain combinations represent subtraction (like IV for 4, IX for 9). The key is to use a mapping of values in descending order, including these special cases
Using an Iterative Approach
This approach uses a predefined dictionary to map Roman numerals to their integer values in descending order. We iteratively subtract the largest possible value from the given number while appending the corresponding Roman numeral
Example
using System;
using System.Collections.Generic;
class Program {
static string ConvertToRomanIterative(int number) {
// Define Roman numeral mappings in descending order
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}");
// Test with another example
number = 58;
romanNumeral = ConvertToRomanIterative(number);
Console.WriteLine($"The Roman numeral for {number} is: {romanNumeral}");
}
}
The output of the above code is
The Roman numeral for 1994 is: MCMXCIV The Roman numeral for 58 is: LVIII
Using StringBuilder for Efficiency
For better performance with string concatenation, we can use StringBuilder instead of regular string concatenation. This approach is more efficient for larger numbers
Example
using System;
using System.Collections.Generic;
using System.Text;
class Program {
static string ConvertToRomanUsingStringBuilder(int number) {
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 = 3999; // Maximum standard Roman numeral
string romanNumeral = ConvertToRomanUsingStringBuilder(number);
Console.WriteLine($"The Roman numeral for {number} is: {romanNumeral}");
}
}
The output of the above code is
The Roman numeral for 3999 is: MMMCMXCIX
Using a Recursive Approach
We can also solve this problem recursively by finding the largest Roman numeral value that fits into the number, then recursively converting the remainder
Example
using System;
using System.Collections.Generic;
class Program {
static 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"}
};
static string ConvertToRomanRecursive(int number) {
if (number == 0) return "";
foreach(var item in romanMap) {
if (number >= item.Key) {
return item.Value + ConvertToRomanRecursive(number - item.Key);
}
}
return "";
}
static void Main() {
int number = 2749;
string romanNumeral = ConvertToRomanRecursive(number);
Console.WriteLine($"The Roman numeral for {number} is: {romanNumeral}");
}
}
The output of the above code is
The Roman numeral for 2749 is: MMDCCXLIX
Comparison
| Approach | Time Complexity | Space Complexity | Best Use Case |
|---|---|---|---|
| Iterative | O(1) | O(1) | Simple implementation |
| StringBuilder | O(1) | O(1) | Better performance for large numbers |
| Recursive | O(1) | O(log n) | Elegant, functional approach |
Common Use Cases
Educational Tools: Converting numbers to Roman numerals for learning purposes and mathematical applications.
Historical Representations: Used in clocks, book chapters, movie credits, and architectural inscriptions for aesthetic or traditional reasons.
Game Development: Roman numerals in scoreboards, levels, or ranks to provide a classical gaming experience.
Document Formatting: Numbering sections, appendices, or footnotes in formal documents and publications.
Conclusion
Converting numbers to Roman numerals in C# can be efficiently accomplished using iterative, StringBuilder-based, or recursive approaches. The iterative method with a dictionary mapping is the most straightforward, while StringBuilder offers better performance for frequent operations. All approaches have O(1) time complexity since Roman numerals have a fixed set of symbols.
