JAVA PROGRAM FOR DENOMINATIONS
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
ALGORITHM:
Step1:
Start.
Step2:
Declare an array d[] of size 9 to store all the specified available
denominations statically.
Step3:
Accept amount from the user.
Step4:
Check whether entered amount is 5 digited, i.e. [amount<1000000].
Step5:
Take a loop ranging from 0 to 9 to check for each note.
Step
6: Divide amount with first denomination (amt/a[0]). The resulting quotient is
the number of required notes for that particular
denomination.
Step7:
Check if quotient is not zero (quo! =0). Display the denomination and number of
required notes in given format.
Step8:
An integer variable ‘total’ is declared in which all quotients are added to
find total number of notes required.
Step
9: Reduce amount using modulo operator to find remaining amount whose
denomination is to be found.
Step10:
Exit loop. Repeat steps 6-9 till amount is reduced to 0.
Step11:
Display total number of required notes.
Step
12: Stop.
import
java.util.*;
class
denominations
{
Scanner sc=new Scanner(System.in);
void main()
{
System.out.print("INPUT: ");
int amt=sc.nextInt();
int d[]={1000,500,100,50,20,10,5,2,1};
if(amt>99999)
{
System.out.println("ivalid");
}
else
{
System.out.println("OUTPUT:");
System.out.println("DENOMINATIONS:");
int s=0,b=amt;
for(int i=0;i<9;i++)
{
int q=amt/d[i];
s=s+q;
if(q!=0)
{
System.out.println(d[i]+"\tx\t"+q+"\t="+d[i]*q);
amt=amt%d[i];
}
}
System.out.println("------------------------------------");
System.out.println("TOTAL\t\t\t="+b);
System.out.println("------------------------------------");
System.out.println("Total
Number of Notes ="+s);
}
}
}
Sample
input/output:
Random
input/output:
Comments
Post a Comment