C program to count total number of notes in given amount

Previous Program Next Program

Write a C program to accept amount as integer and display total number of notes of Rs. 500, 100, 50, 20, 10, 5, 2, 1. How to count the minimum number of notes required for the given amount in C programming. Program to find minimum number of notes required for the given denomination. Logic to find minimum number of denomination for a given amount in C program.

Example

Input

Input amount: 575

Output

Total number of notes: 
500: 1
100: 0
50: 1
20: 1
10: 0
5: 1
2: 0
1: 0

Required knowledge

Basic C programming, If else

Logic to count minimum number of denomination for given amount

There any many optimum algorithms to solve the given problem. For this exercise to make things simple I have used brute force algorithm. Below is the step by step descriptive logic to find minimum number of denomination -

  1. Read amount in some variable say amt.
  2. Check if the amount is greater than 500.
  3. Divide the amount by 500 to get maximum number of 500 notes required. Store the quotient in some variable say note500.
  4. Subtract the resultant amount of 500 notes to get final amount. Which is perform amt = amt - (note500 * 500).
  5. Repeat step 2-4 finding minimum number of notes of 200, 100, 50 and so on.

Let us code solution of the program.

Program to count minimum number of denomination for given amount

/**
 * C program to count minimum number of notes in an amount
 */
 
#include <stdio.h>

int main()
{
    int amount;
    int note500, note100, note50, note20, note10, note5, note2, note1;
    
    // Initialize all notes to 0
    note500 = note100 = note50 = note20 = note10 = note5 = note2 = note1 = 0;


    /* Read amount from user */
    printf("Enter amount: ");
    scanf("%d", &amount);


    if(amount >= 500)
    {
        note500 = amount/500;
        amount -= note500 * 500;
    }
    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;
    }

    printf("Total number of notes = \n");
    printf("500 = %d\n", note500);
    printf("100 = %d\n", note100);
    printf("50 = %d\n", note50);
    printf("20 = %d\n", note20);
    printf("10 = %d\n", note10);
    printf("5 = %d\n", note5);
    printf("2 = %d\n", note2);
    printf("1 = %d\n", note1);

    return 0;
} 
Output
Enter amount: 4673
Total number of notes = 
500 = 9 
100 = 1 
50 = 1 
20 = 1 
10 = 0 
5 = 0 
2 = 1 
1 = 1 

Happy coding ;)

You may also like

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< pre>< code >----Your Source Code---- < / code > < / pre > (Remove spaces from pre and code tags)

3 comments:

  1. Here is an error, please correct it: http://i.imgur.com/CPHtIAx.jpg

    ReplyDelete
  2. Here is an error, please correct it: http://i.imgur.com/CPHtIAx.jpg

    ReplyDelete
    Replies
    1. Thanks, Mohan for observing that. It was a typo.

      Delete