DEV Community

Cover image for Roman to Integer
Ravi Vishwakarma
Ravi Vishwakarma

Posted on

Roman to Integer

Roman numerals, originating from ancient Rome, utilize combinations of Latin letters to represent values:

  • I = 1
  • V = 5
  • X = 10
  • L = 50
  • C = 100
  • D = 500
  • M = 1000

Typically, numerals are written from largest to smallest from left to right. However, to represent numbers like 4 and 9, a subtractive notation is used, where a smaller numeral precedes a larger one, indicating subtraction. For example, IV represents 4 (5 - 1), and IX represents 9 (10 - 1).
MEDIUM

To convert a Roman numeral to an integer, follow these steps:

  1. Initialize a total sum to zero.
  2. Traverse the Roman numeral from left to right.
  3. For each symbol:
  • If its value is less than the value of the next symbol, subtract it from the total.
  • Otherwise, add its value to the total.

This method accounts for both additive and subtractive combinations in Roman numerals.

For example, to convert "MCMXCIV" to an integer:

  1. M (1000)
  2. CM (900)
  3. XC (90)
  4. IV (4)

Adding these values together: 1000 + 900 + 90 + 4 = 1994.

This approach ensures accurate conversion of Roman numerals to their integer equivalents.

Example 1
`
using System;
class Program
{
static void Main()
{
string roman = "MCMXCIV"; // Example Roman numeral
int result = RomanToInt(roman);
Console.WriteLine($"The integer value of {roman} is {result}.");
}

static int RomanToInt(string s)
{
    int total = 0;
    int prevValue = 0;

    foreach (char c in s)
    {
        int currentValue = RomanCharToValue(c);

        if (currentValue > prevValue)
        {
            // Subtract twice the previous value since it was added earlier
            total += currentValue - 2 * prevValue;
        }
        else
        {
            total += currentValue;
        }

        prevValue = currentValue;
    }

    return total;
}

static int RomanCharToValue(char c)
{
    switch (c)
    {
        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: throw new ArgumentException("Invalid Roman numeral character");
    }
}
Enter fullscreen mode Exit fullscreen mode

}

`

Example 2
`
class RomanToInt {
public static int _RomanToInt(string s)
{
int total = 0, prevValue = 0, currentValue = 0;

    for (int i = s.Length - 1; i >= 0; i--)
    {
        switch (s[i])
        {
            case 'I':  currentValue =  1; break;;
            case 'V': currentValue =  5; break;;
            case 'X': currentValue =  10; break;;
            case 'L': currentValue =  50; break;;
            case 'C': currentValue =  100; break;;
            case 'D': currentValue =  500; break;;
            case 'M': currentValue =  1000; break;;
            default: throw new ArgumentException("Invalid Roman numeral character");
        }
        total = (currentValue > prevValue) ? (total + currentValue) : total - currentValue;
        prevValue = currentValue;
    }

    return total;
}
Enter fullscreen mode Exit fullscreen mode

}

`

Thanks for Reading.

Top comments (0)