Roman to decimal conversion

Roman numerals are symbolic characters such as (I, V, X, L, C, D, and M). The combination of which represents a decimal number. Our goal is to convert roman to valid decimal number. For example.

Input  : "XXII"
Output :  22

Input  : "CLXXXIX"
Output :  189

Input  : "DMLXXXII"
Output :  582

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


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







© 2021, kalkicode.com, All rights reserved