PHP Program for Converting Roman Numerals to Decimal Lying Between 1 to 3999


The characters used in an arrangement of numeric notation based on the pre-Roman Roman system are known as Roman numerals. All major symbols are covered in the section below. In this problem, we are given a string of Roman numerals and our task is to convert Roman numerals to decimals in the range of 1 to 3999

Here are some examples and explanations to help you better understand the problem.

Input

str = "DCCCLXXIV"

Output

str = 874

Explanation

DCCC is the Roman representation of 800 as D represents 500 and C represents 100

LXX is the Roman representation of 70 as L represents 50 and X represents 10,

and IV is the Roman representation of 4.

Input

"CMXCIX"

Output

999

Explanation

CM is the Roman representation of 900, M represents 1000 and C represents 100 (100 less than 1000),

Similarly, XC is the Roman representation of 90, C represents 100 and X represents 10 (10 less than 100),

and similarly IX is the Roman representation of 9.

Input

"I"

Output

1

Explanation

I is the Roman representation of 1.

Before we go to the methods, let's take a closer look at the main Roman symbol. Roman has entirely constructed around the following symbol:

SYMBOL VALUE
M 1000
CM 900
D 500
CD 400
C 100
XC 90
L 50
XL 40
X 10
IX 9
V 5
IV 4
I 1

Approach

We have seen the example above for the given Roman numeral string, let us move to the approach.

As per the observation, the Roman numeral symbol follows the descending order to represent the numbers (e.g. M’s come first, the C’s, etc.). However, it also follows subtractive notation in certain situations to prevent 4 characters from continuously repeated (for example XXXX or CCCC) in a row:

  • C comes before D and M to indicate 100 less, Examples:

    -> 400 represent in Roman as CD (hundred less than five hundred)

    -> 900 represent in Roman as CM (hundred less than thousand)

  • X comes before L or C to indicate ten less, Examples:

    -> 40 represent in Roman as XL (ten less than fifty),

    -> 90 represent in Roman as XC (ten less than hundred)

  • I comes before V or X to indicate one less, Examples:

    -> 4 represent in Roman as IV (one less than five),

    -> 9 represent in Roman as IX ( one less than ten)

Let's see the code below for a better understanding of the above approach.

Example

PHP Program to convert Roman Numerals to Decimal Numerals Function “romanValue” is created to return a Roman symbol's value

<?php
function romanValue($ch){
   // intializing the value to store decimal value of roman symbol
   $val = -1;
   if ($ch == 'I')
      $val = 1;
   else if ($ch == 'V')
      $val = 5;
   else if ($ch == 'X')
      $val = 10;
   else if ($ch == 'L')
      $val = 50;
   else if ($ch == 'C')
     $val = 100;
   else if ($ch == 'D')
     $val = 500;
   else if ($ch == 'M')
     $val = 1000; 
   return $val;
} 
// created a function to return decimal value of given roman value
function convertRomanToDecimal(&$str){
   // create variable decValue that we have to return and assign 0 to it 
   $decValue = 0;
   $n = strlen($str); // Getting the size of the given string 
   // calculate decValue while traversing the given string using for loop
   for ($i = 0; $i < $n; $i++) {
      // Store decimal value of romanValue str[i]
      $current = romanValue($str[$i]); 
      // check i+1 char exist
      if ($i+1 < $n) {
         // Store the decimal value of romanValue str[i+1]
         $next = romanValue($str[$i + 1]); 
         // check which value is greater current or next
         if ($current >= $next) {
            // if current value >= next value add value to the decValue
            $decValue = $decValue + $current;
         } else {
            // if current value < next value then add difference of value of next to current to the decValue
            $decValue = $decValue + $next - $current; 
            // Increment the index of the string to point to the next char
            $i++; 
         }
      }
      // If i+1 char not exist
      else {
         // Add current value to the decValue variable
         $decValue = $decValue + $current; 
         // Increment the index of the string to point to the next char
         $i++; 
      }
   }
   // Return decimal value
   return $decValue;
} 
$str ="DCCCLXXIV"; // Given Roman numeral string
// Print the decimal form and call the function of conversion
echo "The decimal Numeral form of the Roman Numeral is " . convertRomanToDecimal($str) . "";
?>

Output

The decimal Numeral form of the Roman Numeral is 874

Time and Space Complexity

The time complexity of the above code is O(N), as only one traverse of the string is required. Where N is the size of the given Roman numeral string. And as there is no extra space used to store anything the time complexity of the above code is O(1).

Conclusion

In this tutorial, we have implemented a PHP program for Converting Roman Numerals To Decimal Lying Between 1 to 3999. We have implemented an approach in which a function is created to get the corresponding decimal value of the Roman value. The time complexity of this approach is O(N) here N is the size of the string and space complexity is O(1) as no extra space is used.

Updated on: 11-Jul-2023

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements