C program to count total number of notes in given amount

In this blog post, we learn how to write a C program to count total number of notes in given amount
?. We will write the C program to count total number of notes in given amount. Logic to find the minimum number of currency notes for a given amount.

Input: 800
Output : Currency  Count 
         500 : 1
         200 : 1
         100 : 1

Input: 2456
Output : Currency  Count
         2000 : 1
         200 : 2
         50 : 1
         5 : 1
         1 : 1

 

 

C program to count total number of notes in given amount

The below C program asks the user to enter an amount and prints the number of notes (of denominations 2000, 500, 200, 100, 50, 20, 10, 5, 1) to be distributed. For example, if the user enters 374, then 1 note of 200, 1 note of 100, 1 note of 50, 1 note of 20, and 4 note of 1 is required.

#include <stdio.h>

#define SIZE 9

int main()
{
    int amount, notes;

    // currency denominations
    int denominations[SIZE] =  { 2000, 500, 200, 100, 50, 20, 10, 5, 1 };

    printf("Enter amount: ");
    scanf("%d", &amount);

    printf("\n");

    for (int i = 0; i < SIZE; i++)
    {
        notes = amount / denominations[i];

        if (notes)
        {
            amount = amount % denominations[i];

            printf("%d * %d = %d \n", notes, denominations[i],
                   notes * denominations[i]);
        }
    }

    return 0;
}

Output:

Enter amount: 374

1 * 200 = 200
1 * 100 = 100
1 * 50 = 50
1 * 20 = 20
4 * 1 = 4

 

Leave a Reply

Your email address will not be published. Required fields are marked *