Hello!

Do anyone know optimal solution for roman numerals to 100?

I made a program just for numerals to 10, but I have no idea how to solve it to 100 optimally cuz if statements are not a very good solution.

static void Main(string[] args)
        {
            int number;
            bool checkValue;

            Console.Write("Enter the number between 1 and 10: ");

            checkValue = int.TryParse(Console.ReadLine(), out number);

            if (checkValue)
            {
                if (number > 1 && number < 10)
                {
                    if (number == 1)
                    {
                        Console.WriteLine("I");
                    }
                    else if (number == 2)
                    {
                        Console.WriteLine("II");
                    }
                    else if (number == 3)
                    {
                        Console.WriteLine("III");
                    }
                    else if (number == 4)
                    {
                        Console.WriteLine("IV");
                    }
                    else if (number == 5)
                    {
                        Console.WriteLine("V");
                    }
                    else if (number == 6)
                    {
                        Console.WriteLine("VI");
                    }
                    else if (number == 7)
                    {
                        Console.WriteLine("VII");
                    }
                    else if (number == 8)
                    {
                        Console.WriteLine("VIII");
                    }
                    else if (number == 9)
                    {
                        Console.WriteLine("IX");
                    }
                    else if (number == 10)
                    {
                        Console.WriteLine("X");
                    }
                }
                else
                {
                    Console.WriteLine("\nERROR! Entered number must be between 1 and 10.");
                }
            }
            else
            {
                Console.WriteLine("\nERROR! Entered value must be a number.");
            }

            Console.ReadKey();
        }

Any help is the most welcome! ;)

Recommended Answers

All 4 Replies

A basic algorithm would be something like:

int temp = inputValue;
string roman = String.Empty;
while (temp > 999)
{
  roman += "M";
  temp -= 1000;
}
while (temp > 899)
{
   roman += "CM";
   temp -= 100;
}
while (temp > 499)
{
   roman += "D";
   temp -= 500;
}
while (temp > 399)
{
   roman += "CD";
   temp -= 100;
}
while (temp > 99)
{
   roman += "C";
   temp -= 100;
}
while (temp > 89)
{
   roman += "XC";
   temp -= 10;
}
// and so on...

There are probably more efficient ways of doing it, but it gives you a general idea.

switch (number) 
{
case 1:
    Console.WriteLine("I");
    break;
case 2:
    Console.WriteLine("II");
    break;
}

Much better than if/else for more than 3 statements.

How about....

string[] romanNumerals = {  //Roman Numerals in Here....    };
int input;

Console.Write("Please enter the number you would like converting to numeral");

input = Convert.ToInt16(Console.ReadLine());

Console.Write(romanNumerals[input]);



What you could also do is put all your roman numerals in to a text file and read each roman numeral in to the array...

anyway think that way may be a bit easier than many many if or switch statements...
Member Avatar for stbuchok

james6754, that is a terrible idea, that would take forever to fill in and you still would probably miss some numbers.

If you can get your hands on "A Programmer's Introduction to C#" I believe there is a full blown example of exactly what you are trying to do (if I remember correctly).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.