using System.Collections.Generic;
using System.Threading.Tasks;
namespace NumberConversions
public int covertMe(string RomanNumeral)
string currentRomanNotation = "";
int secondLetterValue = 0;
int consecutiveCount = 0;
SortedDictionary<char, int> dyRoman = new SortedDictionary<char, int>{{'M', 1000}, {'D', 500}, {'C', 100}, {'L', 50}, {'X', 10}, {'V', 5}, {'I', 1}, {' ', 0}};
RomanNumeral = RomanNumeral.ToUpper().TrimEnd(' ').TrimStart(' ');
if (RomanNumeral.Contains(" "))
throw new ArgumentOutOfRangeException("Please enter a single Roman numeral - no spaces.", new Exception());
for (int x = 0; x < RomanNumeral.Length; x++)
thisChar = RomanNumeral[x];
lastChar = x > 0 ? RomanNumeral[x - 1] : ' ';
consecutiveCount = (thisChar == lastChar) ? consecutiveCount + 1 : 1;
if (dyRoman.TryGetValue(thisChar, out notationValue) && dyRoman.TryGetValue(lastChar, out secondLetterValue))
int subNumber = (notationValue.ToString()[0] == '1') ? (notationValue / 10) : (notationValue / 5);
if (consecutiveCount > 1 && notationValue.ToString()[0] == '5')
throw new ArgumentOutOfRangeException("Invalid Roman numeral - invalid letter repetitions.", new Exception());
if (consecutiveCount > 3)
throw new ArgumentOutOfRangeException("Invalid Roman numeral - specific characters cannot appear in groups of more than three.", new Exception());
if (subNumber > 0 && secondLetterValue > 0 && secondLetterValue != subNumber && secondLetterValue < notationValue)
throw new ArgumentOutOfRangeException("Invalid Roman numeral - possible error in subtractive combinations.", new Exception());
throw new Exception("Invalid character found.");
while (stringPlace < RomanNumeral.Length)
currentRomanNotation = RomanNumeral.Substring(stringPlace, 1);
if (dyRoman.TryGetValue(currentRomanNotation[0], out notationValue))
if ((stringPlace <= (RomanNumeral.Length - 1)) && dyRoman.TryGetValue(RomanNumeral[stringPlace], out secondLetterValue))
if (secondLetterValue > notationValue)
currentRomanNotation += RomanNumeral[stringPlace];
notationValue = secondLetterValue - notationValue;
returnValue += notationValue;
class RomanToarabic : NumericConversions
public static void Main(String[] Args)
Console.WriteLine("Enter the roman numeral: ");
string str = Console.ReadLine();
NumericConversions conversion = new RomanToarabic();
int arabic = conversion.covertMe(str);
Console.WriteLine("In arabic " + arabic);