Convert Roman Numbers to Decimal Numbers


 

Convert Roman Numbers to Decimal Numbers

In this section, you will learn how to convert Roman Numbers to Decimals.

In this section, you will learn how to convert Roman Numbers to Decimals.

Convert Roman Numbers to Decimal Numbers

In the previous section, we have illustrated the conversion of Decimal Numbers to Roman. Here we are doing vice-versa i.e, In this sd For this, we have allowed the user to enter Roman Number like X and convert it into decimal i.e 10.

Here is the code:

import java.util.*;

public class RomanToDecimal {
	public static void main(String args[]) {
		int decimal = 0;
		Scanner input = new Scanner(System.in);
		System.out.print("Enter a Roman Number: ");
		String roman = input.next();
		String romanNumeral = roman.toUpperCase();
		int x = 0;
		do {
			char convertToDecimal = roman.charAt(x);
			switch (convertToDecimal) {
			case 'M':
				decimal += 1000;
				break;

			case 'D':
				decimal += 500;
				break;

			case 'C':
				decimal += 100;
				break;

			case 'L':
				decimal += 50;
				break;

			case 'X':
				decimal += 10;
				break;

			case 'V':
				decimal += 5;
				break;

			case 'I':
				decimal += 1;
				break;
			}
			x++;
		} while (x < romanNumeral.length());
		System.out.println("Decimal Number is: " + decimal);
	}
}

Output:

Enter a Roman Number: XV
Decimal Number is: 15

Ads