In this program, we will learn how to count the total number of notes in the given amount using C++ Programming language.
The user is asked to enter an amount, then this program will calculate the total number of notes that could make up that amount.
So, without further ado, let’s begin this tutorial.
C++ Program to Count Total Number of Notes in Given Amount
// C++ Program to Count Total Number of Notes in Given Amount #include <iostream> using namespace std; int main(){ int i, amount; int arr[9] = {2000, 500, 100, 50, 20, 10, 5, 2, 1}; // Asking for input cout << "Enter the amount: "; cin >> amount; // Calculating total number of notes for (i = 0; i < 9; i++){ cout << arr[i] << " Note: " << amount / arr[i] << endl; amount = amount % arr[i]; } return 0; }
Output
Enter the amount: 18256
2000 Note: 9
500 Note: 0
100 Note: 2
50 Note: 1
20 Note: 0
10 Note: 0
5 Note: 1
2 Note: 0
1 Note: 1
How Does This Program Work ?
int i, temp, amount; int arr[9] = {2000, 500, 100, 50, 20, 10, 5, 2, 1};
In this program, we have declared some int data type variables named i, amount and arr.
// Asking for input cout << "Enter the amount: "; cin >> amount;
Then, the user is asked to enter the amount.
// Calculating total number of notes for (i = 0; i < 9; i++){ cout << arr[i] << " Note: " << amount / arr[i] << endl; amount = amount % arr[i]; }
Now, we calculate the total no. of notes using a simple for loop function.
C++ Program to Count Total Number of Notes Using Functions
// C++ Program to Count Total Number of Notes in Given Amount Using Functions #include <iostream> using namespace std; int CountNotes(int amount){ int i; int arr[9] = {2000, 500, 100, 50, 20, 10, 5, 2, 1}; // Calculating total number of notes for (i = 0; i < 9; i++){ cout << arr[i] << " Note: " << amount / arr[i] << endl; amount = amount % arr[i]; } } int main(){ int amount; // Asking for input cout << "Enter the amount: "; cin >> amount; CountNotes(amount); return 0; }
Output
Enter the amount: 7777
2000 Note: 3
500 Note: 3
100 Note: 2
50 Note: 1
20 Note: 1
10 Note: 0
5 Note: 1
2 Note: 1
1 Note: 0
C++ Program to Count Total Number of Notes Using If Else Statement
// C++ Program to Count Total Number of Notes Using If Else Statement #include <iostream> using namespace std; int main(){ int amount; int note1, note2, note5, note10, note20, note50, note100, note200, note500, note2000; note1 = note2 = note5 = note10 = note20 = note50 = note100 = note200 = note500 = note2000 = 0; // Asking for input cout << "Enter the amount: "; cin >> amount; if (amount >= 2000){ note2000 = amount / 2000; amount -= note2000 * 2000; } if (amount >= 500){ note500 = amount / 500; amount -= note500 * 500; } if (amount >= 200){ note200 = amount / 200; amount -= note200 * 200; } if (amount >= 100){ note100 = amount / 100; amount -= note100 * 100; } if (amount >= 50){ note50 = amount / 50; amount -= note50 * 50; } if (amount >= 20){ note20 = amount / 20; amount -= note20 * 20; } if (amount >= 10){ note10 = amount / 10; amount -= note10 * 10; } if (amount >= 5){ note5 = amount / 5; amount -= note5 * 5; } if (amount >= 2){ note2 = amount / 2; amount -= note2 * 2; } if (amount >= 1){ note1 = amount / 1; amount -= note1 * 1; } // Displaying output cout << "Total number of notes: " << endl; cout << "2000 = " << note2000 << endl; cout << "500 = " << note500 << endl; cout << "200 = " << note200 << endl; cout << "100 = " << note100 << endl; cout << "50 = " << note50 << endl; cout << "20 = " << note20 << endl; cout << "10 = " << note10 << endl; cout << "5 = " << note5 << endl; cout << "2 = " << note2 << endl; cout << "1 = " << note1 << endl; return 0; }
Output
Enter the amount: 5454
Total number of notes:
2000 = 2
500 = 2
200 = 2
100 = 0
50 = 1
20 = 0
10 = 0
5 = 0
2 = 2
1 = 0
Conclusion
I hope after going through this post, you understand how to count total numbers of notes in the given amount using C++ Programming language.
If you have any doubt regarding the program, feel free to contact us in the comment section. We will be delighted to solve your query.
Also Read: