Skip to main content

Command Palette

Search for a command to run...

Java Programming : Count number of Notes in a given amount

Published
2 min readView as Markdown
Java Programming : Count number of Notes in a given amount

Example

Input
enter the amount 1000 
Output
Total number of notes 
500 : 2

Required to Know

Use of Variables, Use of different operators like assignment or arithmetic operators, Use of methods like System. out.print(), and use of next().charAt(0), Use of If-else, and Use of Data types.

Steps

  • Take the amount as input from the user.

  • Now if the amount that we have is more than 500, then we need to divide the amount by 500 to get how many notes are required. So the formula of this will go like this note = amt/500. Similarly, after we get how many notes are required We need to do a subtraction of the resultant amount from the original amount.

  • These particular steps need to get continued many times until we get to know how many notes are required.

Formula

This is to understand how the formula will work to find how many notes are required for a specific amount.

amount = 1000 
note = 1000 / 500 
note = 2 

amount = amount - note * 500 
= 1000 - 2 * 500 
= 0

From this, we can conclude, that for the amount of 1000, we required 2 notes of 500.

Program

import java.util.Scanner;
public class note
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the Amount :");
        int amount = sc.nextInt();
        int n500, n100, n50, n20, n10, n5, n2, n1;
        n500 = n100 = n50 = n20 = n10 = n5 = n2 = n1 = 0;
        if(amount >= 500)
            n500 = amount/500;
            amount -= n500 * 500;
        if(amount >= 100)
            n100 = amount/100;
            amount -= n100 * 100;
        if(amount >= 50)
            n50 = amount/50;
            amount -= n50 * 50;
        if(amount >= 20)
            n20 = amount/20;
            amount -= n20 * 20;
        if(amount >= 10)
            n10 = amount/10;
            amount -= n10 * 10;
        if(amount >= 5)
            n5 = amount/5;
            amount -= n5 * 5;
        if(amount >= 2)
            n2 = amount /2;
            amount -= n2 * 2;
        if(amount >= 1)
            n1 = amount;

        System.out.println("Total Number of Notes");
        System.out.println("500 = "+ n500);
        System.out.println("100 = "+ n100);
        System.out.println("50 = "+ n50);
        System.out.println("20 = "+ n20);
        System.out.println("10 = "+ n10);
        System.out.println("5 = "+ n5);
        System.out.println("2 = "+ n2);
        System.out.println("1 = "+ n1);
    }
}

More from this blog

TheCodingcoder

60 posts

Empowering the coding community by simplifying complex tasks.