Monday , 27 February 2017
Home » Array Related Programs » Program for displaying the Denominations of an Amount

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].

Example:

INPUT:   14788

OUTPUT:

DENOMINATIONS:

1000     x     14    =   14000
500       x     1      =   500
100       x     2      =   200
50         x     1      =   50
20         x     1      =   20
10         x     1      =   10
5           x     1      =   5
2           x     1      =   2
1           x     1      =   1
————————————–
TOTAL                =   14788
————————————–
Total Number of Notes = 23

[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:

Enter any Amount: 14856

DENOMINATIONS:

1000     x     14    =   14000
500       x     1      =   500
100       x     3      =   300
50         x     1      =   50
5           x     1      =   5
1           x     1      =   1
————————————–
TOTAL                =   14856
————————————–
Total Number of Notes = 21

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

Next, i becomes 1,
count=amount/den[1];
or, count = 856/500;
or, count = 1
Since, 1 !=0 is satisfied, the denomination 500 is printed along with the number of notes in the given pattern.
and, amount=amount%den[1];
or, amount = 856%500
or, amount = 356

Next, i becomes 2,
count=amount/den[2];
or, count = 356/100;
or, count = 3
Since, 3 !=0 is satisfied, the denomination 100 is printed along with the number of notes in the given pattern.
and, amount=amount%den[2];
or, amount = 356%100
or, amount = 56

Next, i becomes 3,
count=amount/den[3];
or, count = 56/50;
or, count = 1
Since, 1 !=0 is satisfied, the denomination 50 is printed along with the number of notes in the given pattern.
and, amount=amount%den[3];
or, amount = 56%50
or, amount = 6

Next, i becomes 4,
count=amount/den[4];
or, count = 6/20;
or, count = 0
Since, 0 !=0 is Not satisfied, the denomination is not printed.
and, amount=amount%den[4];
or, amount = 6%20
or, amount = 6

Next, i becomes 5,
count=amount/den[5];
or, count = 6/10;
or, count = 0
Since, 0 !=0 is Not satisfied, the denomination is not printed.
and, amount=amount%den[5];
or, amount = 6%10
or, amount = 6

Next, i becomes 6,
count=amount/den[6];
or, count = 6/5;
or, count = 1
Since, 1 !=0 is satisfied, the denomination 50 is printed along with the number of notes in the given pattern.
and, amount=amount%den[6];
or, amount = 6%5
or, amount = 1

Next, i becomes 7,
count=amount/den[7];
or, count = 1/2;
or, count = 0
Since, 0 !=0 is Not satisfied, the denomination is not printed.
and, amount=amount%den[7];
or, amount = 1%2
or, amount = 1

Next, i becomes 8,
count=amount/den[8];
or, count = 1/1;
or, count = 1
Since, 1 !=0 is satisfied, the denomination 50 is printed along with the number of notes in the given pattern.
and, amount=amount%den[8];
or, amount = 1%1
or, amount = 0

Next, when i becomes 9, the for loop will stop.

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.

That is it ! You have the denominations in a really simple manner.

Check Also

[Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

8 comments

  1. Thank you!!!It’s really helped my project.

  2. thanx dude

  3. I copied this program and when I compiled it, it said “<identifier> expected” and the line selected was  System.out.println(“——————————–“); why is it showing so? Please help.

  4. This program is not working 🙁

Leave a Reply to Jack Spicer Cancel reply

Your email address will not be published. Required fields are marked *