Friday , 20 April 2018
Latest Posts
Home » Array Related Programs » Program on Decimal to Roman Number Conversion [Method 1]

# Program on Decimal to Roman Number Conversion [Method 1]

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

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

#### Question:

Write a program in Java to find the Roman equivalent of any Decimal number entered by the user. [The number entered should be within the Range 1-3999]

#### Brief Note on Roman Numerals:

The Roman numerals follow this basic pattern,
1000 = M, 900 = CM, 500 = D, 400 = CD, 100 = C, 90 = XC, 50 = L, 40 = XL, 10 = X, 9 = IX, 5 = V, 4 = IV, 1 = I

The symbols “I”, “X”, “C”, and “M” can be repeated three times in succession, but no more. i.e. 234 can be represented as CCXXXIV, but 244 cannot be written as CCXXXXIV [Since we cannot repeat X more than 3 times successively].

(They may only appear more than three times if they appear non-sequentially, such as XXXIX.) “D”, “L”, and “V” can never be repeated.

#### Solution:

```/**
* The class Dec2Roman_Method1 takes a Decimal number as Input and finds its Roman Equivalent.
* This is Method 1
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class Dec2Roman_Method1
{
public static void main(String args[]) throws IOException
{
System.out.print("Enter a Number : ");

if(num>0 && num<4000) //checking whether the number entered is within the range [1-3999]
{

/*Saving the Roman equivalent of the thousand, hundred, ten and units place of a decimal number*/
String thou[]={"","M","MM","MMM"};
String hund[]={"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"};
String ten[]={"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
String unit[]={"","I","II","III","IV","V","VI","VII","VIII","IX"};

/*Finding the digits in the thousand, hundred, ten and units place*/
int th=num/1000;
int h=(num/100)%10;
int t=(num/10)%10;
int u=num%10;

/*Displaying equivalent roman number*/
System.out.println("Roman Equivalent= "+thou[th]+hund[h]+ten[t]+unit[u]);
}

/*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 : 3482
Roman Equivalent = MMMCDLXXXII

#### 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 4 String Arrays:

● The 1st Array contains the Roman Equivalents of 1000, 2000 and 3000.
● The 2nd Array contains the Roman Equivalents of 100, 200, 300, 400, 500, 600, 700, 800 and 900.
● The 3rd Array contains the Roman Equivalents of 10, 20, 30, 40, 50, 60, 70, 80 and 90.
● The 4th Array contains the Roman Equivalents of 1, 2, 3, 4, 5, 6, 7, 8 and 9.

The 1st value in each of the above Array is “” i.e. nothing, because we don’t print anything if we get a ‘0’ at either thousand’s, or hundred’s, or ten’s or unit’s place.

4. We then find the digits present in the thousand’s, hundred’s, ten’s and unit’s place by using simple mathematical calculations which are as follows:
Digit at thousand’s place = num/1000;
Digit at hundreds’s place = (num/100)%10;
Digit at ten’s place = (num/10)%10;
Digit at ten’s place = num%10;

5. To get the Roman equivalent of the digit at:
● thousand’s place, we look up the Array thou [ ] at the index equal to the digit at thousand’s place.
● hundred’s place, we look up the Array hund [ ] at the index equal to the digit at hundred’s place.
● ten’s place, we look up the Array ten [ ] at the index equal to the digit at ten’s place.
● unit’s place, we look up the Array unit [ ] at the index equal to the digit at unit’s place.

6. The result is then displayed together.

That is it – A good example of how array makes life easier for programmers.

#### Working:

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

Digit at thousand’s place :
th = num/1000;
th = 3482/1000;
th = 3
Digit at hundreds’s place :
h = (num/100)%10;
h = (3482/100)%10;
h = (34%10);
h = 4
Digit at ten’s place :
t = (num/10)%10;
t = (3482/10)%10;
t = (348%10);
t = 8
Digit at ten’s place :
u = num%10;
u = 3482%10;
u = 2

Then we look up the corresponding Arrays,

thou[th] = thou[3] = MMM
hund[h] = hund[4] = CD
ten[t] = ten[8] = LXXX
unit[u] = unit[2] = II

Therefore, printing the value of: thou[th]+hund[h]+ten[t]+unit[u] gives, MMMCDLXXXII which is the Roman equivalent of 3482.

## ISC 2017 Computer Science Solution + Examiner’s Comments – From the Council

Solution of ISC 2017 Computer science Paper as provided by the Council for the Indian School Certificate Examinations.