DEV Community

Daniel Yunus
Daniel Yunus

Posted on

C# Program to convert Roman numerals to decimal integers.

Introduction

Hi, I am Daniel Yunus 😃. I am a game developer 🎮 and a data analyst 💻, I am here to learn and share. In this article we will solve a problem to convert roman numerals to decimal integers.


Description

Roman numerals are represented using 7 symbols as follows:

Roman Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

A roman numeral has the following properties:

  • It is written from left to right in descending order.
    Example : 2 is written as 'II' which is 'I'+'I' or 1+1, 6 is written as 'VI' which is 'V'+'I' or 5+1.

  • A character cannot be repeated four times in succession. Subtraction notation is used to represent such instances.
    Example : 4 is not written as 'IIII', rather it is written as 'IV'. In this case 'I' is written before 'V' which denotes 'I' should be subtracted from 'V' i.e., 'V'-'I'(5-1=4).

There are 6 instances where subtraction notation is used:

  • 'I' is placed before 'V'(5) to make 'IV'(4).
  • 'I' is placed before 'X'(10) to make 'IX'(9).
  • 'X' is placed before 'L'(50) to make 'XL'(40).
  • 'X' is placed before 'C'(100) to make 'XC'(90).
  • 'C' is placed before 'D'(500) to make 'CD'(400).
  • 'C' is placed before 'M'(1000) to make 'CM'(900).

Examples

Input Output
II 2
XL 40
MCMXCVI 1996

Solution

Algorithm:

  1. Iterate through each symbol/character in Roman numeral string starting from index 0.
  2. Get corresponding integer value of the current symbol.
  3. If current value of symbol is greater than next value of symbol, then add this value to the total result. Else, subtract the current symbol value from the total result, if the next symbol value is less than 10 times the current value.
using System;

public class RomanToDecimal
{
    private int GetIntegerValue(char symbol)
    {
        switch(symbol)
        {
            case 'I':
                return 1;
            case 'V':
                return 5;
            case 'X':
                return 10;
            case 'L':
                return 50;
            case 'C':
                return 100;
            case 'D':
                return 500;
            case 'M':
                return 1000;
            default: return -1;
        }
    }

    private int Convert(string roman)
    {
        var sum = 0;
        for(var i=0; i<roman.Length; i++)
        {
            var current = GetIntegerValue(roman[i]);
            if(current == -1)
                return -1;

            if((i+1)<roman.Length)
            {
                var next = GetIntegerValue(roman[i+1]);
                if(next == -1)
                    return -1;

                if(next > current)
                {
                    if(next > 10*current)
                        return -1;

                    sum -= current;
                    continue;
                }
            }
            sum+=current;
        }
        return sum;
    }

    public static void Main(string[] args)
    {
        var obj = new RomanToDecimal();

        var input = new string[] {"II", "XL", "MCMXCVI", "XD"};
        foreach(string s in input)
        {
           var result = obj.Convert(s); 
           if(result == -1)
                Console.WriteLine ("Please input a valid Roman Numeral");
           else Console.WriteLine("Integer Value For "+s+" is : "+result);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Thank You. Follow for more such content 😃 💻.

Top comments (0)