Skip to main content

Decimal to roman numeral converter in c#

Csharp program for Decimal to roman numeral converter. Here mentioned other language solution.

// Include namespace system
using System; 
//  C# program for
//  Conversion from Decimal to roman number
public class Perform
{
    // Display roman value of n
    public static void result(int n)
    {
        switch (n)
         {
            // Test Cases
            case 1:
                Console.Write("I");
                break;
            case 4:
                Console.Write("IV");
                break;
            case 5:
                Console.Write("V");
                break;
            case 9:
                Console.Write("IX");
                break;
            case 10:
                Console.Write("X");
                break;
            case 40:
                Console.Write("XL");
                break;
            case 50:
                Console.Write("L");
                break;
            case 90:
                Console.Write("XC");
                break;
            case 100:
                Console.Write("C");
                break;
            case 400:
                Console.Write("CD");
                break;
            case 500:
                Console.Write("D");
                break;
            case 900:
                Console.Write("DM");
                break;
            case 1000:
                Console.Write("M");
                break;
        }
    }
    public static long select(long number, 
                               int[] collection, int size)
    {
        var n = 1;
        var i = 0;
        for (
        i = 0; i < size; ++i)
        {
            if (number >= collection[i])
            {
                n = collection[i];
            }
            else
            {
                break;
            }
        }
        result(n);
        // Reduce the value of number
        return number - n;
    }
    public static void romanNo(long number)
    {
        if (number <= 0)
        {
            // When is not a natural number
            return;
        }
        // Base case collection
        int[] collection =
        {1, 4, 5, 9, 10, 40, 50, 90, 100, 400, 500, 900, 1000};
        // Get the size of collection
        var size = collection.Length;
        Console.Write(" " + number + " : ");
        while (number > 0)
        {
            number = select(number, collection, size);
        }
        // Add new line
        Console.WriteLine();
    }
    public static void Main(String[] args)
    {
        // Test Case
        romanNo(10);
        romanNo(18);
        romanNo(189);
        romanNo(604);
        romanNo(982);
        romanNo(3000);
    }
}

Output

 10 : X
 18 : XVIII
 189 : CLXXXIX
 604 : DCIV
 982 : DMLXXXII
 3000 : MMM




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment