An algorithm to compute the smallest number of notes required to combine the input amount

/*
Author:- Mayukh Datta
Website:- https://thecoducer.github.io
*/
#include<stdio.h>
void main()
{
    long amt;
    int note2000,note500,note100,note50,note20,note10,note5,note2,note1;

    //Initialize all note variables to 0
    note2000=note500=note100=note50=note20=note10=note5=note2=note1=0;

    printf("Enter an amount:- ");
    scanf("%ld", &amt);

    printf("\nDenominations:- \n");

    if(amt>=2000)
    {
        note2000=amt/2000;
        amt=amt-(note2000*2000);

        if(note2000>0)
            printf("2000*%d\n", note2000);

    }
    if(amt>=500)
    {
        note500=amt/500;
        amt=amt-(note500*500);

        if(note500>0)
            printf("500*%d\n", note500);
    }
    if(amt>=100)
    {
        note100=amt/100;
        amt=amt-(note100*100);

        if(note100>0)
            printf("100*%d\n", note100);
    }
    if(amt>=50)
    {
        note50=amt/50;
        amt=amt-(note50*50);

        if(note50>0)
            printf("50*%d\n", note50);
    }
    if(amt>=20)
    {
        note20=amt/20;
        amt=amt-(note20*20);

        if(note20>0)
            printf("20*%d\n", note20);
    }
    if(amt>=10)
    {
        note10=amt/10;
        amt=amt-(note10*10);

        if(note10>0)
            printf("10*%d\n", note10);
    }
    if(amt>=5)
    {
        note5=amt/5;
        amt=amt-(note5*5);

        if(note5>0)
            printf("5*%d\n", note5);
    }
    if(amt>=2)
    {
        note2=amt/2;
        amt=amt-(note2*2);

        if(note2>0)
            printf("2*%d\n", note2);
    }
    if(amt>=1)
    {
        note1=amt;

        if(note1>0)
            printf("1*%d\n", note1);
    }
}
Advertisements

#c, #indian-currency, #least-notes-required-algorithm