Program on Decimal to Roman Number Conversion - 2

This is the Second Method of Converting a Decimal Number entered by the user into its Roman Equivalent. For Method 1, kindly Click Here: Decimal to Roman Conversion – Method 1

[Note: The number entered should be within the Range 1-3999]

For Brief Note on Roman Number System, kindly Read Method 1


Solution:


/*
[Converting a Decimal Number entered by the user into its Roman Equivalent - Method 2]
[www.javaforschool.com]
*/

import java.io.*;
class Dec2Roman_Method2
{
public static void main() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int num;
String str="";
System.out.print("Enter a Number : ");
num=Integer.parseInt(br.readLine()); //accepting decimal number
/*Arrays storing the unique symbols of Roman Number System
and also the corresponding decimal equivalents in the second array*/
String roman[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
int decimal[] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
if(num>0 && num<4000) //checking whether the number entered is within the range [1-3999]
{
for (int i = 0; i < 13; i++) // i < 13 because the total unique numbers stored in the above array = 13
{
/*The while loop is for printing repeated digits like XXX for 30 etc
and is also calculating the equivalent Roman number by adding the corresponding Roman Digits from the Array to the String str*/
while (num >= decimal[i])
{
num = num-decimal[i];
str = str+roman[i];
}
}
System.out.println("Roman Equivalent = "+str); //Printing the Roman equivalent
}
/*Displaying an error message if the number entered is out of range*/
else
System.out.println("\nYou entered a number out of Range.\nPlease enter a number in the range [1-3999]");
}
}

Output:

Enter a Number : 863
Roman Equivalent = DCCCLXIII

Explanation:

We have solved the above program through the following steps:

1. First we enter a decimal number and then check whether it lies in the range [1-3999].

2. If it does not lie in the above range, a proper Error message is displayed.

3. If it lies within the above range, then we create 2 Arrays:

● The 1st Array is a String Array which contains all the unique Roman symbols, which are 13 in number.
● The 2nd Array is an Integer Array and contains the Decimal equivalent of all the unique Roman symbols.

4. The for loop then runs from 0-12 (<13) times checking for all those decimal numbers stored in the decimal [ ] array for which we have unique Roman symbols

5. The while loop then checks whether the entered number is greater than or less than or equal to those decimal numbers stored in the decimal [ ] array for which we have unique Roman symbols.

6. If the number is greater than or equal to those decimal numbers for which we have unique Roman symbols which we have unique Roman symbols, then that decimal number is subtracted from the original number and the equivalent Roman symbol is added (joined) to the variable ‘str’.

7. If the number after subtraction is again greater than that unique decimal number, then it is again subtracted and the corresponding roman symbol is again joined to the variable ‘str’

Note: Point 7 is for numbers where the symbols “I”, “X”, “C”, and “M” are to be repeated again and again for a maximum of 3 times for numbers such as 2,3,20,30,200,300,2000,3000 etc.

8. The for loop stops after checking for all the 13 unique decimal numbers for which we have a unique Roman equivalent.

9. The result which is stored in the String ‘str’ is then displayed.

Working:

If the number entered is, num = 863, then,

For, i = 0,

863 >= decimal[0]
863 >= 1000 (not satisfied) ‘i’ will increase

For, i = 1,

863 >= decimal[1]
863 >= 900 (not satisfied) ‘i’ will increase

For, i = 2,

863 >= decimal[2]
863 >= 500 (satisfied)
n = 863-500 = 363
str = str + roman[2]
str = “”+”D”
str = “D”

Again while loop will check, 363 >= 500 (not satisfied) ‘i’ will increase

For, i = 3,

363 >= decimal[3]
363 >= 400 (not satisfied) ‘i’ will increase

For, i = 4,

363 >= decimal[4]
363 >= 100 (satisfied)
n = 363-100 = 263
str = str + roman[4]
str = “D”+”C”
str = “DC”

Again while loop will check, 263 >= 100 (satisfied)
n = 263-100 = 163
str = str + roman[4]
str = “DC”+”C”
str = “DCC”

Again while loop will check, 163 >= 100 (satisfied)
n = 163-100 = 63
str = str + roman[4]
str = “DCC”+”C”
str = “DCCC”

Again while loop will check, 63 >= 100 (not satisfied) ‘i’ will increase

For, i = 5,

63 >= decimal[5]
63 >= 90 (not satisfied) ‘i’ will increase

For, i = 6,

63 >= decimal[6]
63 >= 50 (satisfied)
n = 63-50 = 13
str = str + roman[6]
str = “DCCC”+”L”
str = “DCCCL”

Again while loop will check, 13 >= 50 (not satisfied) ‘i’ will increase

For, i = 7,

13 >= decimal[7]
13 >= 40 (not satisfied) ‘i’ will increase

For, i = 8,

13 >= decimal[8]
13 >= 10 (satisfied)
n = 13-10 = 3
str = str + roman[8]
str = “DCCCL”+”X”
str = “DCCCLX”

Again while loop will check, 3 >= 10 (not satisfied) ‘i’ will increase

For, i = 9,

3 >= decimal[9]
3 >= 9 (not satisfied) ‘i’ will increase

For, i = 10,

3 >= decimal[10]
3 >= 5 (not satisfied) ‘i’ will increase

For, i = 11,

3 >= decimal[11]
3 >= 4 (not satisfied) ‘i’ will increase

For, i = 12,

3 >= decimal[12]
3 >= 1 (satisfied)
n = 3-1 = 2
str = str + roman[12]
str = “DCCCLX”+”I”
str = “DCCCLXI”

Again while loop will check, 2 >= 1 (satisfied)
n = 2-1 = 1
str = str + roman[12]
str = “DCCCLXI”+”I”
str = “DCCCLXII”

Again while loop will check, 1 >= 1 (satisfied)
n = 1-1 = 0
str = str + roman[12]
str = “DCCCLXII”+”I”
str = “DCCCLXIII”

Again while loop will check, 0 >= 1 (not satisfied) ‘i’ will increase

When i = 13, the for loop will stop (because for loop runs for < 13), and we have the final result in the String variable ‘str’


Therefore, printing the value of variable ‘str’ gives, DCCCLXIII which is the Roman equivalent of 863.

No comments:

Post a Comment