Currency Denomination Program in Java and Python
Currency denomination program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.
Question:
A bank intends to design a program to display the denomination of an input amount up to 5 digits. The available denominations are rupees 1000, 500, 100, 50, 20, 10, 5, 2 and 1.
Design a program to accept the amount from the user and display its break-up in descending order of denomination, giving preference to the highest denomination available. Display only the denominations used, along with the total number of notes.
Algorithm:
Step 1: Start.
Step 2: Store all denominations in descending order in array den.
Step 3: Accept the amount from the user.
Step 4: If the amount is less than 1 or greater than 99999, display invalid amount and stop.
Step 5: Store a copy of the original amount for printing the total later.
Step 6: Initialize totalNotes = 0.
Step 7: Run a loop through every denomination in the den array.
Step 8: For each denomination, calculate count = amount / den[i].
Step 9: If count is not zero, print the denomination, count and product.
Step 10: Add count to totalNotes.
Step 11: Update the remaining amount using amount = amount % den[i].
Step 12: After all denominations are checked, print the original amount and total number of notes.
Step 13: Stop.
Explanation:
The program breaks an amount into currency denominations by always trying the largest denomination first. The array den stores denominations in descending order: 1000, 500, 100 and so on down to 1. This order is important because the question expects preference to be given to higher denominations wherever possible.
For each denomination, integer division gives the number of notes or coins of that denomination that can be used. For example, if the current amount is 14788 and the denomination is 1000, then 14788 / 1000 gives 14. This means fourteen 1000-rupee notes can be used. If the count is not zero, the program prints that denomination line along with the value contributed by it.
After processing one denomination, the remaining amount is found using the remainder operator. The statement amount = amount % den[i] removes the value already represented by the current denomination. The next loop pass then works only on what is left. The variable copy preserves the original amount so that the final total can be printed even after amount has been reduced to 0. The variable totalNotes keeps a running count of all notes used.
The method is a greedy approach: at each stage it chooses the largest possible denomination. Because Indian currency denominations are arranged to work naturally this way, the greedy choice gives a compact denomination list with preference to higher notes.
Java Program:
/**
* The class Denominations calculates and displays the denominations of an amount
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.Scanner;
class Denominations
{
public static void main(String args[])
{
Scanner sc = new Scanner(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=sc.nextInt();
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
}
}Equivalent Python Program:
den = [1000, 500, 100, 50, 20, 10, 5, 2, 1]
amount = int(input("Enter any Amount: "))
# The question allows an amount up to 5 digits.
if amount < 1 or amount > 99999:
print("Invalid Amount")
else:
# copy stores the original amount because amount is reduced in the loop.
copy = amount
total_notes = 0
print("DENOMINATIONS:")
for i in range(len(den)):
count = amount // den[i]
# Only denominations with non-zero count are displayed.
if count != 0:
print(den[i], "x", count, "=", den[i] * count)
total_notes = total_notes + count
amount = amount % den[i]
print("--------------------------------")
print("TOTAL =", copy)
print("--------------------------------")
print("Total Number of Notes =", total_notes)Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.