Well, suppose that a cashier owes a customer some change and in that cashier’s drawer are quarters (25¢), dimes (10¢), nickels (5¢), and pennies (1¢). The problem to be solved is to decide which coins and how many of each to hand to the customer
if some customer is owed 41¢, the biggest first bite that can be taken is 25¢ 41 - 25 = 16 another 25¢ bite would be too big cashier would move on to a bite of size 10¢, leaving him or her with a 6¢ problem At that point the cashier calls for one 5¢ bite followed by one 1¢ bite
#include <cs50.h>
#include <stdio.h>
int main(void)
{
float f = get_float("Enter Cash: ");
int q;
int d;
int n;
int p;
float quarter = 0.25;
float dimes = 0.10;
float nickels = 0.05;
float pennies = 0.01;
while ( f != 0)
{
if (f >= quarter){
f = f - quarter;
q = q + 1;
} else if (f >= dimes && f < quarter) {
f = f - dimes;
d = d + 1;
} else if (f >= nickels && f < dimes) {
f = f - nickels;
n = n + 1;
} else {
f = f - pennies;
p = p + 1;
}
printf ("Quarter: %d \n Dimes %d \n Nickels %d \n Pennies %d \n", q,
d, n, p);
}
}
$ ./cash
Enter Cash: 6
Quarter: 32768
Dimes -1230737968
Nickels 0
Pennies 4205168
Quarter: 32769
Dimes -1230737968
Nickels 0
Pennies 4205168
Quarter: 32770
Dimes -1230737968
Nickels 0
Pennies 4205168
Quarter: 32771
Dimes -1230737968
Nickels 0
Pennies 4205168
Quarter: 32772