Educational Blog: Making Change Solution

Tuesday, 3 March 2015

Making Change Solution

#include <stdio.h>
#include<conio.h>

int change(float total, int *hundred, int *fifty, int *twenty, int *ten, int *five, int *one);
void print(float total, int hundred, int fifty, int twenty, int ten, int five, int one);

int main(void)
{
             int hundred,fifty, twenty, ten, five, one,sum=0;
             float total;
             clrscr();
             printf("\nPlease enter an amount of money: \n");
             scanf("%f", &total);
             change(total,&hundred, &fifty, &twenty, &ten, &five, &one);
             print(total, hundred, fifty, twenty, ten, five, one);
             sum=hundred+fifty+twenty+ten+five+one;
             printf("No of cions are:%d ",sum);
             getch();
}



int change(float total, int *hundred, int *fifty, int *twenty, int *ten, int *five, int *one)
{
            if( total >= 100.00)
                        *hundred =(total /100.00);
            if( total >= 50.00 )
                        *fifty = (total- (*hundred * 100.00))/50.00;
             if( total >= 20.00 )
                        *twenty = (total - (*hundred * 100.00)- (*fifty * 50.00)) / 20.00;
             if( total >= 10.00 )
*ten = (total - (*hundred * 100.00) - (*fifty * 50.00) - (*twenty *     20.00)) / 10.00;
             if( total >= 5.00 )
*five = (total - (*hundred * 100.00) - (*fifty * 50.00) - (*twenty * 20.00) - (*ten * 10.00)) / 5.00;
            if( total >= 1.00 )
*one = (total - (*hundred * 100.00) - (*fifty * 50.00) - (*twenty * 20.00) - (*ten * 10.00)-(*five * 5)) / 1.00;
                        return 0;
}
void print(float total, int hundred, int fifty, int twenty, int ten, int five ,int one)
{
            printf("\nTOTAL VALUE ENTERED: Rs %.2f", total);
            printf("\n%3d No of 100 Rs coin \n", hundred);
            printf("\n%3d  No of 50 Rs coin \n", fifty);
            printf("\n%3d No of 20 Rs coin \n", twenty);
            printf("\n%3d No of 10 Rs coin \n", ten);
            printf("\n%3d No of 5 Rs coin \n", five);
           printf("\n%3d No of 1 Rs coin \n", one);
}


No comments:

Post a Comment