Java Programs to Find the Minimum Number of Currency Notes to Make an Amount N

Currency notes

Java program to find the minimum number of currency notes which sum up to the given amount N, is given here. The quantity of each type of note for the amount is also shown.



Page content(s):

1. Program & output

1.1. Java




1. Program to Find the Minimum Number of Currency Notes to Make an Amount N




1.1. Java Program to Find the Minimum Number of Currency Notes to Make an Amount N

Code has been copied
/************************************************
	         alphabetacoder.com
Java program to find the minimum number of
currency notes which sum upto the given amount N
*************************************************/

import java.util.Scanner;

class Main {
    public static void main(String args[]) {
        // declare object of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        int N, t, k, i;
        // declare an array to store different currency value
        int currency[] = {500, 200, 100, 50, 20, 10, 5, 2, 1};

        //take input of the order of the matrix
        System.out.print("Enter an amount: ");
        N = sc.nextInt();

        // initialize
        t = 0;

        // new line
        System.out.println("");

        // count the total no of currency unit
        for (i = 0; i < currency.length; i++) {
            k = N / currency[i];
            // display result for current unit
            System.out.println(currency[i] + " required: " + k);
            // add to counter
            t = t + k;
            // calculate remaining amount
            N = N - k * currency[i];
        }

        //display total
        System.out.println("\nMinimum no. of notes required: " + t);
    }
}

Output


Enter an amount: 1270


500 required: 2

200 required: 1

100 required: 0

50 required: 1

20 required: 1

10 required: 0

5 required: 0

2 required: 0

1 required: 0


Minimum no. of notes required: 6