Posted on by Kalkicode
Code Conversion

Roman to decimal conversion

Roman numerals are a numeral system that originated in ancient Rome. They use combinations of letters from the Latin alphabet to represent numeric values. Each letter represents a specific value, and the values are added together to create the final number. The conversion from Roman numerals to decimal numbers is a common problem in computer science and mathematics.

Problem Statement

The task is to write a program that takes a Roman numeral as input and converts it into its corresponding decimal representation. The Roman numeral can contain the letters 'I', 'V', 'X', 'L', 'C', 'D', and 'M', representing the numbers 1, 5, 10, 50, 100, 500, and 1000, respectively. The program should then output the decimal number.

Explanation with Suitable Example

Let's take the Roman numeral "XXII" as an example. To convert this into a decimal number, we follow these steps:

  1. Initialize a variable 'number' to 0 to store the final decimal value.
  2. Start traversing the Roman numeral from left to right.
  3. For each letter, get its corresponding decimal value using a lookup table. For 'X', the value is 10.
  4. Check the next letter to the right (if it exists).
  5. If the value of the next letter is greater than the current one (e.g., 'I' before 'X'), subtract the current value from the 'number'.
  6. If the value of the next letter is less than or equal to the current one (e.g., 'X' before 'I'), add the current value to the 'number'.
  7. Continue the process until all letters have been processed.
  8. Finally, the 'number' will contain the decimal representation of the Roman numeral.

Pseudocode

function getData(char ch):
    switch (ch):
        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

function romanToDecimal(String romanText):
    size = length of romanText
    if size <= 0:
        return
    number = 0
    for i = 0 to size-1:
        submit1 = getData(romanText[i])
        if submit1 == -1:
            return
        if i+1 < size:
            submit2 = getData(romanText[i + 1])
            if submit2 == -1:
                return
            if submit2 > submit1:
                number = number - submit1
            else:
                number = number + submit1
        else:
            number = number + submit1
    print "Roman Text " + romanText + " : Decimal Number " + number

Algorithm Explanation:

  1. The getData(char ch) function takes a Roman letter as input and returns its corresponding decimal value.
  2. The romanToDecimal(String romanText) function takes a Roman numeral as input and converts it to a decimal number using the following steps: a. Initialize number as 0 to store the result. b. Traverse the Roman numeral from left to right using a loop. c. For each letter, call the getData() function to get its decimal value. d. Check if the next letter (if it exists) has a greater value than the current letter. If so, subtract the current value from the number. e. If the next letter has a value less than or equal to the current letter, add the current value to the number. f. Continue this process until all letters have been processed. g. The number will now hold the decimal representation of the Roman numeral. h. Print the result.

Resultant Output Explanation

Let's take three examples and understand their conversion to decimal numbers.

  1. Roman Numeral: "XXII"

    • Start with number = 0.
    • Process 'X', number += 10.
    • Process 'X', number += 10.
    • Process 'I', number += 1.
    • Final number = 10 + 10 + 1 = 21.
    • Output: "Roman Text XXII : Decimal Number 22"
  2. Roman Numeral: "CLXXXIX"

    • Start with number = 0.
    • Process 'C', number += 100.
    • Process 'L', number += 50.
    • Process 'X', number += 10.
    • Process 'X', number += 10.
    • Process 'X', number += 10.
    • Process 'I', number += 1.
    • Process 'X', number -= 10 (since 'X' is before 'I').
    • Process 'X', number -= 10 (since 'X' is before 'I').
    • Process 'I', number += 1.
    • Final number = 100 + 50 + 10 + 10 + 10 + 1 - 10 - 10 + 1 = 180.
    • Output: "Roman Text CLXXXIX : Decimal Number 189"
  3. Roman Numeral: "DMLXXXII"

    • Start with number = 0.
    • Process 'D', number += 500.
    • Process 'M', number += 1000.
    • Process 'L', number += 50.
    • Process 'X', number += 10.
    • Process 'X', number += 10.
    • Process 'I', number += 1.
    • Process 'I', number -= 10 (since 'I' is before 'X').
    • Process 'I', number -= 10 (since 'I' is before 'X').
    • Final number = 500 + 1000 + 50 + 10 + 10 + 1 - 10 - 10 = 582.
    • Output: "Roman Text DMLXXXII : Decimal Number 582"

Code Solution

Here given code implementation process.

/*
 Java program
 Roman to decimal conversion
*/
class RomanToDecimal
{
    public int getData(char ch)
    {
        switch (ch)
        {
            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;
        }
        return -1;
    }
    public void decimalNumber(String romanText)
    {
        int size = romanText.length();
        if (size <= 0)
        {
            return;
        }
        long number = 0;
        int submit1 = 0, submit2 = 0;
        for (int i = 0; i < size; ++i)
        {
            submit1 = getData(romanText.charAt(i));
            if (submit1 == -1)
            {
                return;
            }
            if (i + 1 < size)
            {
                //Get next element of location i+1
                submit2 = getData(romanText.charAt(i + 1));
                if (submit2 == -1)
                {
                    return;
                }
                // Compare current and next element result
                // Current node i and next node is i+1
                if (submit2 > submit1)
                {
                    number = number - submit1;
                }
                if (submit2 <= submit1)
                {
                    number = number + submit1;
                }
            }
            else
            {
                number += submit1;
            }
        }
        System.out.print(" Roman Text " + romanText + 
                         " : Decimal Number " + number + "\n");
    }
    public static void main(String[] args)
    {
        RomanToDecimal task = new RomanToDecimal();
        // Test Example
        task.decimalNumber("XXII");
        task.decimalNumber("CLXXXIX");
        task.decimalNumber("DMLXXXII");
    }
}

Output

 Roman Text XXII : Decimal Number 22
 Roman Text CLXXXIX : Decimal Number 189
 Roman Text DMLXXXII : Decimal Number 582
// Include namespace system
using System;
/*
 Csharp program
 Roman to decimal conversion
*/
public class RomanToDecimal
{
    public int getData(char ch)
    {
        switch (ch)
        {
            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;
        }
        return -1;
    }
    public void decimalNumber(String romanText)
    {
        int size = romanText.Length;
        if (size <= 0)
        {
            return;
        }
        long number = 0;
        int submit1 = 0;
        int submit2 = 0;
        for (int i = 0; i < size; ++i)
        {
            submit1 = this.getData(romanText[i]);
            if (submit1 == -1)
            {
                return;
            }
            if (i + 1 < size)
            {
                //Get next element of location i+1
                submit2 = this.getData(romanText[i + 1]);
                if (submit2 == -1)
                {
                    return;
                }
                // Compare current and next element result
                // Current node i and next node is i+1
                if (submit2 > submit1)
                {
                    number = number - submit1;
                }
                if (submit2 <= submit1)
                {
                    number = number + submit1;
                }
            }
            else
            {
                number += submit1;
            }
        }
        Console.Write(" Roman Text " + romanText + " : Decimal Number " + number + "\n");
    }
    public static void Main(String[] args)
    {
        RomanToDecimal task = new RomanToDecimal();
        // Test Example
        task.decimalNumber("XXII");
        task.decimalNumber("CLXXXIX");
        task.decimalNumber("DMLXXXII");
    }
}

Output

 Roman Text XXII : Decimal Number 22
 Roman Text CLXXXIX : Decimal Number 189
 Roman Text DMLXXXII : Decimal Number 582
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
 C++ program
 Roman to decimal conversion
*/
class RomanToDecimal
{
    public: int getData(char ch)
    {
        switch (ch)
        {
            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;
        }
        return -1;
    }
    void decimalNumber(string romanText)
    {
        int size = romanText.length();
        if (size <= 0)
        {
            return;
        }
        long number = 0;
        int submit1 = 0;
        int submit2 = 0;
        for (int i = 0; i < size; ++i)
        {
            submit1 = this->getData(romanText[i]);
            if (submit1 == -1)
            {
                return;
            }
            if (i + 1 < size)
            {
                //Get next element of location i+1
                submit2 = this->getData(romanText[i + 1]);
                if (submit2 == -1)
                {
                    return;
                }
                // Compare current and next element result
                // Current node i and next node is i+1
                if (submit2 > submit1)
                {
                    number = number - submit1;
                }
                if (submit2 <= submit1)
                {
                    number = number + submit1;
                }
            }
            else
            {
                number += submit1;
            }
        }
        cout << " Roman Text " << romanText 
             << " : Decimal Number " << number << "\n";
    }
};
int main()
{
    RomanToDecimal *task = new RomanToDecimal();
    // Test Example
    task->decimalNumber("XXII");
    task->decimalNumber("CLXXXIX");
    task->decimalNumber("DMLXXXII");
    return 0;
}

Output

 Roman Text XXII : Decimal Number 22
 Roman Text CLXXXIX : Decimal Number 189
 Roman Text DMLXXXII : Decimal Number 582
//C Program
//Roman to decimal conversion
#include <stdio.h>

int get_data(char ch)
{
    switch (ch)
    {
        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;
    }
    return -1;
}
void roman_number(char roman_text[], int size)
{
    if (size <= 0)
    {
        return;
    }
    long long number = 0;
    int submit1 = 0, submit2 = 0;
    for (int i = 0; i < size; ++i)
    {
        submit1 = get_data(roman_text[i]);
        if (submit1 == -1)
        {
            return;
        }
        if (i + 1 < size)
        {
            //Get next element of location i+1
            submit2 = get_data(roman_text[i + 1]);
            if (submit2 == -1)
            {
                return;
            }
            // Compare current and next element result
            // Current node i and next node is i+1
            if (submit2 > submit1)
            {
                number = number - submit1;
            }
            if (submit2 <= submit1)
            {
                number = number + submit1;
            }
        }
        else
        {
            number += submit1;
        }
    }
    printf(" Roman Text %s : Decimal Number %lld\n", roman_text, number);
}
int main()
{
    char text1[] = "XXII";
    int size = sizeof(text1) / sizeof(text1[0]) - 1;
    //Test Case
    roman_number(text1, size);
    char text2[] = "CLXXXIX";
    size = sizeof(text2) / sizeof(text2[0]) - 1;
    roman_number(text2, size);
    char text3[] = "DMLXXXII";
    size = sizeof(text3) / sizeof(text3[0]) - 1;
    roman_number(text3, size);
    return 0;
}

Output

 Roman Text XXII : Decimal Number 22
 Roman Text CLXXXIX : Decimal Number 189
 Roman Text DMLXXXII : Decimal Number 582
package main
import "fmt"
/*
 Go program
 Roman to decimal conversion
*/

func getData(ch byte) int64 {
    switch ch {
        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
    }
    return -1
}
func decimalNumber(romanText string) {
    var size int = len(romanText)
    if size <= 0 {
        return
    }
    var number int64 = 0
    var submit1 int64 = 0
    var submit2 int64 = 0
    for i := 0 ; i < size ; i++ {
        submit1 = getData(romanText[i])
        if submit1 == -1 {
            return
        }
        if i + 1 < size {
            //Get next element of location i+1
            submit2 = getData(romanText[i + 1])
            if submit2 == -1 {
                return
            }
            // Compare current and next element result
            // Current node i and next node is i+1
            if submit2 > submit1 {
                number = number - submit1
            }
            if submit2 <= submit1 {
                number = number + submit1
            }
        } else {
            number += submit1
        }
    }
    fmt.Print(" Roman Text ", romanText, " : Decimal Number ", number, "\n")
}
func main() {

    // Test Example
    decimalNumber("XXII")
    decimalNumber("CLXXXIX")
    decimalNumber("DMLXXXII")
}

Output

 Roman Text XXII : Decimal Number 22
 Roman Text CLXXXIX : Decimal Number 189
 Roman Text DMLXXXII : Decimal Number 582
<?php
/*
 Php program
 Roman to decimal conversion
*/
class RomanToDecimal
{
    public  function getData($ch)
    {
        switch ($ch)
        {
            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;
        }
        return -1;
    }
    public  function decimalNumber($romanText)
    {
        $size = strlen($romanText);
        if ($size <= 0)
        {
            return;
        }
        $number = 0;
        $submit1 = 0;
        $submit2 = 0;
        for ($i = 0; $i < $size; ++$i)
        {
            $submit1 = $this->getData($romanText[$i]);
            if ($submit1 == -1)
            {
                return;
            }
            if ($i + 1 < $size)
            {
                //Get next element of location i+1
                $submit2 = $this->getData($romanText[$i + 1]);
                if ($submit2 == -1)
                {
                    return;
                }
                // Compare current and next element result
                // Current node i and next node is i+1
                if ($submit2 > $submit1)
                {
                    $number = $number - $submit1;
                }
                if ($submit2 <= $submit1)
                {
                    $number = $number + $submit1;
                }
            }
            else
            {
                $number += $submit1;
            }
        }
        echo(" Roman Text ".$romanText.
            " : Decimal Number ".strval($number).
            "\n");
    }
}

function main()
{
    $task = new RomanToDecimal();
    // Test Example
    $task->decimalNumber("XXII");
    $task->decimalNumber("CLXXXIX");
    $task->decimalNumber("DMLXXXII");
}
main();

Output

 Roman Text XXII : Decimal Number 22
 Roman Text CLXXXIX : Decimal Number 189
 Roman Text DMLXXXII : Decimal Number 582
/*
 Node JS program
 Roman to decimal conversion
*/
class RomanToDecimal
{
    getData(ch)
    {
        switch (ch)
        {
            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;
        }
        return -1;
    }
    decimalNumber(romanText)
    {
        var size = romanText.length;
        if (size <= 0)
        {
            return;
        }
        var number = 0;
        var submit1 = 0;
        var submit2 = 0;
        for (var i = 0; i < size; ++i)
        {
            submit1 = this.getData(romanText.charAt(i));
            if (submit1 == -1)
            {
                return;
            }
            if (i + 1 < size)
            {
                //Get next element of location i+1
                submit2 = this.getData(romanText.charAt(i + 1));
                if (submit2 == -1)
                {
                    return;
                }
                // Compare current and next element result
                // Current node i and next node is i+1
                if (submit2 > submit1)
                {
                    number = number - submit1;
                }
                if (submit2 <= submit1)
                {
                    number = number + submit1;
                }
            }
            else
            {
                number += submit1;
            }
        }
        console.log(" Roman Text " + 
                             romanText + " : Decimal Number " + 
                             number );
    }
}

function main()
{
    var task = new RomanToDecimal();
    // Test Example
    task.decimalNumber("XXII");
    task.decimalNumber("CLXXXIX");
    task.decimalNumber("DMLXXXII");
}
main();

Output

 Roman Text XXII : Decimal Number 22
 Roman Text CLXXXIX : Decimal Number 189
 Roman Text DMLXXXII : Decimal Number 582
# Python 3 program
# Roman to decimal conversion
class RomanToDecimal :
    def getData(self, ch) :
        if (ch == 'I') :
            return 1
        
        elif(ch == 'V') :
            return 5
        
        elif(ch == 'X') :
            return 10
        
        elif(ch == 'L') :
            return 50
        
        elif(ch == 'C') :
            return 100
        
        elif(ch == 'D') :
            return 500
        
        elif(ch == 'M') :
            return 1000
        
        return -1
    
    def decimalNumber(self, romanText) :
        size = len(romanText)
        if (size <= 0) :
            return
        
        number = 0
        submit1 = 0
        submit2 = 0
        i = 0
        while (i < size) :
            submit1 = self.getData(romanText[i])
            if (submit1 == -1) :
                return
            
            if (i + 1 < size) :
                # Get next element of location i+1
                submit2 = self.getData(romanText[i + 1])
                if (submit2 == -1) :
                    return
                
                #  Compare current and next element result
                #  Current node i and next node is i+1
                if (submit2 > submit1) :
                    number = number - submit1
                
                if (submit2 <= submit1) :
                    number = number + submit1
                
            else :
                number += submit1
            
            i += 1
        
        print(" Roman Text", romanText ,
              ": Decimal Number ", number )
    

def main() :
    task = RomanToDecimal()
    #  Test Example
    task.decimalNumber("XXII")
    task.decimalNumber("CLXXXIX")
    task.decimalNumber("DMLXXXII")

if __name__ == "__main__": main()

Output

 Roman Text XXII : Decimal Number  22
 Roman Text CLXXXIX : Decimal Number  189
 Roman Text DMLXXXII : Decimal Number  582
# Ruby program
# Roman to decimal conversion
class RomanToDecimal 
    def getData(ch) 
        if (ch == 'I')
        
            return 1
        elsif(ch == 'V')
        
            return 5
        elsif(ch == 'X')
        
            return 10
        elsif(ch == 'L')
        
            return 50
        elsif(ch == 'C')
        
            return 100
        elsif(ch == 'D')
        
            return 500
        elsif(ch == 'M')
        
            return 1000
        end

        return -1
    end

    def decimalNumber(romanText) 
        size = romanText.length
        if (size <= 0) 
            return
        end

        number = 0
        submit1 = 0
        submit2 = 0
        i = 0
        while (i < size) 
            submit1 = self.getData(romanText[i])
            if (submit1 == -1) 
                return
            end

            if (i + 1 < size) 
                # Get next element of location i+1
                submit2 = self.getData(romanText[i + 1])
                if (submit2 == -1) 
                    return
                end

                #  Compare current and next element result
                #  Current node i and next node is i+1
                if (submit2 > submit1) 
                    number = number - submit1
                end

                if (submit2 <= submit1) 
                    number = number + submit1
                end

            else
 
                number += submit1
            end

            i += 1
        end

        print(" Roman Text ", romanText ," : Decimal Number ", number ,"\n")
    end

end

def main() 
    task = RomanToDecimal.new()
    #  Test Example
    task.decimalNumber("XXII")
    task.decimalNumber("CLXXXIX")
    task.decimalNumber("DMLXXXII")
end

main()

Output

 Roman Text XXII : Decimal Number 22
 Roman Text CLXXXIX : Decimal Number 189
 Roman Text DMLXXXII : Decimal Number 582
/*
 Scala program
 Roman to decimal conversion
*/
class RomanToDecimal()
{
    def getData(ch: Char): Int = {
              if (ch == 'I')
        {
            return 1;
        }
        else if (ch == 'V')
        {
            return 5;
        }
        else if (ch == 'X')
        {
            return 10;
        }
        else if (ch == 'L')
        {
            return 50;
        }
        else if (ch == 'C')
        {
            return 100;
        }
        else if (ch == 'D')
        {
            return 500;
        }
        else if (ch == 'M')
        {
            return 1000;
        }
        return -1;
    }
    def decimalNumber(romanText: String): Unit = {
        var size: Int = romanText.length();
        if (size <= 0)
        {
            return;
        }
        var number: Long = 0;
        var submit1: Int = 0;
        var submit2: Int = 0;
        var i: Int = 0;
        while (i < size)
        {
            submit1 = getData(romanText.charAt(i));
            if (submit1 == -1)
            {
                return;
            }
            if (i + 1 < size)
            {
                //Get next element of location i+1
                submit2 = getData(romanText.charAt(i + 1));
                if (submit2 == -1)
                {
                    return;
                }
                // Compare current and next element result
                // Current node i and next node is i+1
                if (submit2 > submit1)
                {
                    number = number - submit1;
                }
                if (submit2 <= submit1)
                {
                    number = number + submit1;
                }
            }
            else
            {
                number += submit1;
            }
            i += 1;
        }
        print(" Roman Text " + romanText + " : Decimal Number " + number + "\n");
    }
}
object Main
{
    def main(args: Array[String]): Unit = {
        var task: RomanToDecimal = new RomanToDecimal();
        // Test Example
        task.decimalNumber("XXII");
        task.decimalNumber("CLXXXIX");
        task.decimalNumber("DMLXXXII");
    }
}

Output

 Roman Text XXII : Decimal Number 22
 Roman Text CLXXXIX : Decimal Number 189
 Roman Text DMLXXXII : Decimal Number 582
import Foundation;
/*
 Swift 4 program
 Roman to decimal conversion
*/
class RomanToDecimal
{
    func getData(_ ch: Character) -> Int
    {
      if (ch == "I")
        {
            return 1;
        }
        else if (ch == "V")
        {
            return 5;
        }
        else if (ch == "X")
        {
            return 10;
        }
        else if (ch == "L")
        {
            return 50;
        }
        else if (ch == "C")
        {
            return 100;
        }
        else if (ch == "D")
        {
            return 500;
        }
        else if (ch == "M")
        {
            return 1000;
        }
        return -1;
    }
    func decimalNumber(_ textNumber: String)
    {
        let romanText = Array(textNumber);
        let size: Int = romanText.count;
      
        if (size <= 0)
        {
            return;
        }
        var number: Int = 0;
        var submit1: Int = 0;
        var submit2: Int = 0;
        var i: Int = 0;
        while (i < size)
        {
            submit1 = self.getData(romanText[i]);
            if (submit1 == -1)
            {
                return;
            }
            if (i + 1 < size)
            {
                //Get next element of location i+1
                submit2 = self.getData(romanText[i + 1]);
                if (submit2 == -1)
                {
                    return;
                }
                // Compare current and next element result
                // Current node i and next node is i+1
                if (submit2 > submit1)
                {
                    number = number - submit1;
                }
                if (submit2 <= submit1)
                {
                    number = number + submit1;
                }
            }
            else
            {
                number += submit1;
            }
            i += 1;
        }
        print(" Roman Text ", textNumber ," : Decimal Number ", number );
    }
}
func main()
{
    let task: RomanToDecimal = RomanToDecimal();
    // Test Example
    task.decimalNumber("XXII");
    task.decimalNumber("CLXXXIX");
    task.decimalNumber("DMLXXXII");
}
main();

Output

 Roman Text  XXII  : Decimal Number  22
 Roman Text  CLXXXIX  : Decimal Number  189
 Roman Text  DMLXXXII  : Decimal Number  582
/*
 Kotlin program
 Roman to decimal conversion
*/
class RomanToDecimal
{
    fun getData(ch: Char): Int
    {
      if (ch == 'I')
        {
            return 1;
        }
        else if (ch == 'V')
        {
            return 5;
        }
        else if (ch == 'X')
        {
            return 10;
        }
        else if (ch == 'L')
        {
            return 50;
        }
        else if (ch == 'C')
        {
            return 100;
        }
        else if (ch == 'D')
        {
            return 500;
        }
        else if (ch == 'M')
        {
            return 1000;
        }
        return -1;
    }
    fun decimalNumber(romanText: String): Unit
    {
        val size: Int = romanText.length;
        if (size <= 0)
        {
            return;
        }
        var number: Long = 0;
        var submit1: Int;
        var submit2: Int;
        var i: Int = 0;
        while (i < size)
        {
            submit1 = this.getData(romanText.get(i));
            if (submit1 == -1)
            {
                return;
            }
            if (i + 1 < size)
            {
                //Get next element of location i+1
                submit2 = this.getData(romanText.get(i + 1));
                if (submit2 == -1)
                {
                    return;
                }
                // Compare current and next element result
                // Current node i and next node is i+1
                if (submit2 > submit1)
                {
                    number = number - submit1;
                }
                if (submit2 <= submit1)
                {
                    number = number + submit1;
                }
            }
            else
            {
                number += submit1;
            }
            i += 1;
        }
        print(" Roman Text " + romanText + 
              " : Decimal Number " + number + "\n");
    }
}
fun main(args: Array < String > ): Unit
{
    val task: RomanToDecimal = RomanToDecimal();
    // Test Example
    task.decimalNumber("XXII");
    task.decimalNumber("CLXXXIX");
    task.decimalNumber("DMLXXXII");
}

Output

 Roman Text XXII : Decimal Number 22
 Roman Text CLXXXIX : Decimal Number 189
 Roman Text DMLXXXII : Decimal Number 582

Time Complexity of the Code

The time complexity of the provided code is O(n), where 'n' is the length of the Roman numeral. This is because we traverse the entire Roman numeral once to convert it into a decimal number. The lookup in the switch-case block has constant time complexity, and the for loop iterates over the entire Roman numeral, leading to a linear time complexity overall.

Finally

In this article, we discussed the problem of converting Roman numerals to decimal numbers. We provided a step-by-step explanation of the code, along with suitable examples. The algorithm for the conversion was presented in pseudocode, independent of any specific programming language. Finally, we analyzed the time complexity of the code, which is linear and depends on the length of the Roman numeral. The provided Java program efficiently performs the Roman to decimal conversion and can be utilized in various applications that involve working with Roman numerals.

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