C & C++ programs to find the minimum number of currency notes which sum up to the given amount N, are given here. The quantity of each type of note for the amount is also shown.
1. Program to Find the Minimum Number of Currency Notes to Make an Amount N
1.1. C Program to Find the Minimum Number of Currency Notes to Make an Amount N
Code has been copied
/************************************************ alphabetacoder.com C program to find the minimum number of currency notes which sum upto the given amount N *************************************************/ #include <stdio.h> int main() { // 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 printf("Enter an amount: "); scanf("%d", & N); // initialize t = 0; // new line printf("\n"); // count the total no of currency unit for (i = 0; i < sizeof(currency) / sizeof(currency[0]); i++) { k = N / currency[i]; // display result for current unit printf("%d required: %d\n", currency[i], k); // add to counter t = t + k; // calculate remaining amount N = N - k * currency[i]; } //display total printf("\nMinimum no. of notes required: %d\n", t); return 0; }
Output
Enter an amount: 927
500 required: 1
200 required: 2
100 required: 0
50 required: 0
20 required: 1
10 required: 0
5 required: 1
2 required: 1
1 required: 0
Minimum no. of notes required: 6
1.2. C++ Program to Find the Minimum Number of Currency Notes to Make an Amount N
Code has been copied
/************************************************ alphabetacoder.com C++ program to find the minimum number of currency notes which sum upto the given amount N *************************************************/ #include <iostream> using namespace std; int main() { // 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 cout << "Enter an amount: "; cin >> N; // initialize t = 0; // new line cout << endl; // count the total no of currency unit for (i = 0; i < sizeof(currency) / sizeof(currency[0]); i++) { k = N / currency[i]; // display result for current unit cout << currency[i] << " required: " << k << endl; // add to counter t = t + k; // calculate remaining amount N = N - k * currency[i]; } //display total cout << "\nMinimum no. of notes required: " << t << endl; return 0; }
Output
Enter an amount: 1343
500 required: 2
200 required: 1
100 required: 1
50 required: 0
20 required: 2
10 required: 0
5 required: 0
2 required: 1
1 required: 1
Minimum no. of notes required: 8