Skip to content

Instantly share code, notes, and snippets.

@vksvicky
Last active February 23, 2016 11:23
Show Gist options
  • Save vksvicky/a051984e5e16db5bcde8 to your computer and use it in GitHub Desktop.
Save vksvicky/a051984e5e16db5bcde8 to your computer and use it in GitHub Desktop.
using System;
public class RomanNumeralsGenerator {
public string ToRoman(int number)
{
var romanNumerals = new string[][]
{
new string[]{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}, // ones
new string[]{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}, // tens
new string[]{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}, // hundreds
new string[]{"", "M", "MM", "MMM"} // thousands
};
// split integer string into array and reverse array
var intArr = number.ToString().Reverse().ToArray();
var len = intArr.Length;
var romanNumeral = "";
var i = len;
// starting with the highest place (for 3046, it would be the thousands
// place, or 3), get the roman numeral representation for that place
// and add it to the final roman numeral string
while (i-- > 0)
{
romanNumeral += romanNumerals[i][intArr[i].ToString()];
}
return romanNumeral;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment