C Program to print Total number of Notes in given Amount
Counting Your Cash: A C Program for Currency Denominations
Imagine walking into a bustling marketplace, laden with purchases and eager to settle the bill. But your pockets hold a jumble of bills and coins, and calculating the exact change feels like a mental marathon. Fear not, for the power of C programming can come to your rescue! This guide will equip you with a C program that effortlessly calculates the total number of notes required for any given amount, bringing order to your monetary chaos.
1. Understanding the Challenge
Our objective is to create a program that takes an amount as input and outputs the corresponding number of notes for each denomination available (e.g., 2000, 500, 100, 50, 20, 10, 5, and 1). This involves iterating through each denomination, dividing the amount by its value, and assigning the quotient as the number of notes for that denomination. Any remaining amount after iterating through all denominations represents coins (assuming denominations below 1 are considered coins).
2. Building the Program
Here’s the C program that translates this logic into action:
C
#include <stdio.h>
int main() {
// Declare variables
int amount, noteCount[8]; // array to store note counts
int denominations[] = {2000, 500, 100, 50, 20, 10, 5, 1}; // denomination values
// Input amount
printf("Enter amount: ");
scanf("%d", &amount);
// Loop through denominations
for (int i = 0; i < 8; i++) {
noteCount[i] = amount / denominations[i]; // calculate note count
amount = amount % denominations[i]; // update remaining amount
}
// Print note counts
printf("\nNumber of notes:\n");
for (int i = 0; i < 8; i++) {
if (noteCount[i] > 0) {
printf("%d notes of %d rupees\n", noteCount[i], denominations[i]);
}
}
return 0;
}
Explanation:
- We include the
stdio.h
header for input/output operations. - We declare variables for the amount, an array to store note counts for each denomination, and an array containing the denomination values themselves.
- The user inputs the desired amount.
- A loop iterates through each denomination in the
denominations
array. - Inside the loop, the amount is divided by the current denomination, and the quotient is assigned to the corresponding element in the
noteCount
array. This represents the number of notes required for that denomination. - The remaining amount (obtained using the modulo operator
%
) is carried over to the next iteration. - After iterating through all denominations, the program prints the note counts for each denomination with a value greater than 0.
3. Running the Program
Compile and run the program. Enter any desired amount, and witness the magic! The program will neatly display the number of notes required for each denomination, making settling bills a breeze.
Example:
Enter amount: 868
Number of notes:
3 notes of 200 rupees
1 note of 500 rupees
1 note of 100 rupees
1 note of 50 rupees
3 notes of 10 rupees
1 note of 5 rupees
3 notes of 1 rupee
4. Extending the Functionality
This program serves as a foundation, and you can customize it further based on your needs. Here are some ideas:
- Multiple currencies: Modify the program to handle different currencies by creating separate arrays for denominations and currency symbols.
- Coin denominations: If desired, you can incorporate coin denominations by adding them to the
denominations
array and adjusting the loop logic accordingly. - User-defined denominations: Allow users to input their own denomination values for specific scenarios.
Remember, the versatility of C gives you the power to tailor this program to suit your unique requirements. Experiment, explore, and let your code become your financial confidante!