Share on Google+Share on Google+

Convert Roman Numbers to Decimal Numbers

Advertisement
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

Advertisements

Posted on: July 12, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.