Program for displaying the Denominations of an Amount

Question:

A bank intends to design a program to display the denomination of an input amount, up to 5 digits. The available denomination with the bank are of rupees 1000 , 500 , 100 , 50 , 20 , 10 , 5 , 2 , and 1.

Design a program to accept the amount from the user and display the break-up in descending order of denomination. (i.e. preference should be given to the highest denomination available) along with the total number of notes. [Note: Only the denomination used, should be displayed].

[Note: This question came in ISC 2010 Practical Examination with a little addition of printing the amount in words according to the digits. Like, for 14788, it should print One Four Seven Eight Eight.]

Solution:

/**
* The class Denominations calculates and displays the denominations of an amount
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.io.*;
class Denominations
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int den[]={1000,500,100,50,20,10,5,2,1}; //storing all the denominations in an array
System.out.print("Enter any Amount: "); //Entering an amount
int amount=Integer.parseInt(br.readLine());
int copy=amount; //Making a copy of the amount
int totalNotes=0,count=0;
System.out.println("\nDENOMINATIONS: \n");
for(int i=0;i<9;i++) //Since there are 9 different types of notes, hence we check for each note.
{
count=amount/den[i]; // counting number of den[i] notes
if(count!=0) //printing that denomination if the count is not zero
{
System.out.println(den[i]+"\tx\t"+count+"\t= "+den[i]*count);
}
totalNotes=totalNotes+count; //finding the total number of notes
amount=amount%den[i]; //finding the remaining amount whose denomination is to be found
}
System.out.println("--------------------------------");
System.out.println("TOTAL\t\t\t= "+copy); //printing the total amount
System.out.println("--------------------------------");
System.out.println("Total Number of Notes\t= "+totalNotes); //printing the total number of notes
}
}

Note: “\t” is used for giving “Tab spaces” and “\n” is used for going to the new line.

Output:

Working and Explanation:

We create an integer array called den [] which stores all the given denominations in descending order.

If the user enters 14856 as the amount, then, amount = 14856

We then, create a copy of the original amount, as the original amount will be reduced to zero after its denominations are calculated. So, we are keeping a backup copy of the original amount.
Hence, copy = 14856

The variable totalNotes is for counting the total number of notes calculated of each denominations, and the variable, count is for calculating number of notes of each denominations.

Now, the loop, for(int i=0;i<9;i++) will execute. The loop goes on from 0 till <9 because that is the number of denominations we have been given (9).

In the for loop we calculate the number of notes of each denominations and store it in the variable count.

If this count is not zero, we are printing the denominations along with the number of notes.

When i = 0,
count=amount/den[i];
or, count = 14856/1000;
or, count = 14
Since, 14 !=0 is satisfied, the denomination 1000 is printed along with the number of notes in the given pattern.
and, amount=amount%den[i];
or, amount = 14856%1000
or, amount = 856

The for loop is printing all those denominations whose number of notes is Not equal to zero, (in the pattern given as example) i.e. 1000 (no. of notes = 14) , 500 (no. of notes = 1) , 100 (no. of notes = 3) , 50 (no. of notes = 1) , 5 (no. of notes = 1), and finally 1 (no.of notes = 1), because it is given in the question that we have to display only those denominations which have been used.

The printing statement: System.out.println(den[i]+”\tx\t”+count+”\t= “+den[i]*count); works as follows:

When i=0,
System.out.println(den[0]+”\tx\t”+count+”\t= “+den[0]*count);
Now, den[0]=1000 and count=14. The phrase, “\tx\t” leaves a tab space, then prints ‘x’ and then again leaves a tab space. The phrase “\t=” leaves a tab space and prints ‘=’.

Hence, the above line prints: 1000 x 14 = 14000

So, the above printing statement works in a similar manner for all those denominations whose count is not zero.

Within the for loop, we are also counting the total number of notes used by adding the values stored in the count to the variable totalNotes. Hence, the variable ‘totalNotes’ will add 14+1+3+1+1+1 = 21

In the end we are also printing the Total amount along with the total number of notes.

ICSE | ISC Sample Papers

In this section we will be providing you with the Sample/Guess papers of ICSE (Computer Applications) and ISC (Computer Science) which can be easily downloaded. We hope that the students will benefit from these resources.