# Thread: Determing the amount of change(money)

1. ## Determing the amount of change(money)

The output of the program should look like this:

This program computes the change to be given
for a purchase paid for with a \$20 bill
The purchase must be less than or equal to \$20

Enter amount of purchase: 0.01
Purchase amount is 0.01
Change due is 19.99

\$10.00 1
\$5.00 1
\$1.00 4
Quarters 3
Dimes 2
Pennies 4
I was able to get the inputs to work correctly by using if and else statements. The part I'm having trouble with is how to determine and print out the amount needed for each bill and coin like in the sample output above. I know the maximum amount that can be used for each bill/coin, but I'm not sure what methods to use to determine how to print out the correct amount for a certain amount of change.

So basically, I would like to know how I would go about printing out the amount of coins/bills? I first assumed that I could use some type of loop, but I didn't know how to go on about it. Then I thought about doing if and else statements, but it was going to be a lot which would end up confusing me.

Here's what I've gotten so far, which is basically asking for user input and printing out the change only. Not the bills/coins.

Code:
```#include <stdio.h>

int main()
{
double amount;
double change = 0;

printf("This program computes the change to be given\n");
printf("for purchases paid for with a \$20.\n");
printf("Purchase must be between \$0.00 and \$20.00\n\n");

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

printf("\nPurchase amount: \$%.2f\n", amount);

if (amount > 0.00 && amount < 20.00)
{
change = 20.00 - amount;
printf("Change due: \$%.2f\n\n", change);
}

else if (amount == 20.00)
{
printf("No change due\n");
}

else
{
printf("Please enter an amount greater than \$0.00 and\n");
printf("less than or equal to \$20.00\n");
return 1;
}

return 0;
}```
I would just like some tips on how to go on about printing out the amount of bills/coins for a certain amount of change. Something to get me started.

Thanks.

2. Originally Posted by Kyeong
I would just like some tips on how to go on about printing out the amount of bills/coins for a certain amount of change. Something to get me started.

Thanks.
Do you yourself know how to make change? If you were working at a counter and I bought \$7.18 and handed you a \$20, what would you do?

3. I would make a structure like this:

Example:
Code:
```struct change
{
short penny;
short nickel;
short dime;
short quarter;
short fiddy_cent_piece;
short sacajawea;
short dollar;
short two;
short lincoln;
short ten;
short twenty;
short fiddy;
short franklin;
};```
Or similar. Some of those may not be technically necessary. But I figured I would be thorough.

4. Originally Posted by master5001
I would make a structure like this:

Example:
Code:
```struct change
{
short penny;
short nickel;
short dime;
short quarter;
short fiddy_cent_piece;
short sacajawea;
short dollar;
short two;
short lincoln;
short ten;
short twenty;
short fiddy;
short franklin;
};```
Or similar. Some of those may not be technically necessary. But I figured I would be thorough.
Surely those should be unsigned?

5. Originally Posted by tabstop
Do you yourself know how to make change? If you were working at a counter and I bought \$7.18 and handed you a \$20, what would you do?
Did you even read my whole post or looked at my code? I'm not asking how to get change from a \$20 bill. I'm asking how I would go about printing the amount of each bill and coins needed to give out for a certain amount of change.

6. Originally Posted by Kyeong
Did you even read my whole post or looked at my code?
I read the whole thing, yes (surprising for me, it is). What's your point?

If it makes you feel better, I'll let you skip ahead the part you programmed. You are working at a counter, and you need to hand me \$12.82 in change. How do you do it?

7. His code was perfectly valid and as per usual very similar to what I would have asked. And surely they should be unsigned, and even chars for higher denominations.

12.82 how? Would you expect the semi-cute teenage zit-faced girl at carls jr to give you back two fives, two twos, 77 pennies and a nickel? Albeit typical of the people I deal with routinely, you would want:

one \$10, two \$1's (or one \$2), three \$0.25's (or one \$0.50 and one \$0.25), one \$0.05, and two \$0.01's, right?

How did I do that? Did my years in retail make me some sort of super genious? Is there a technique to this madness? What is the technique? I betcha it's less difficult to demonstrate mathematically than you may think.

8. Did you say you needed the maximum number of bills/coins? *double takes*

Umm.... give them back only pennies ever? Anyway, if it were me I would ditch the fractions and just multiply everything by 100. That way a penny has an actual value of 1 instead of 0.01. Why? You could eliminate all the floats, thats why

9. Originally Posted by master5001
one \$10, two \$1's (or one \$2), three \$0.25's (or one \$0.50 and one \$0.25), one \$0.05, and two \$0.01's, right?
Ok, I guess I need to re-write my question.

This is a program which would ask for user input. So how would I know how much of each bill and coin would be needed to give out to the customer? How will the program know? I know myself, but I'm not the program. So that's why I'm asking here how to do it. Do I use if and else statements? Divide each value to determine how much of each bill or coin needs to be given?

Like what I quoted above, how would the program itself know what to print that? That's what I'm trying to make the program do.

10. Originally Posted by Kyeong
I know myself
How do you know? Can you write it down? If you can write it down, can you write it down as a flowchart?

11. Originally Posted by Kyeong
Ok, I guess I need to re-write my question.

This is a program which would ask for user input. So how would I know how much of each bill and coin would be needed to give out to the customer? How will the program know? I know myself, but I'm not the program. So that's why I'm asking here how to do it. Do I use if and else statements? Divide each value to determine how much of each bill or coin needs to be given?

Like what I quoted above, how would the program itself know what to print that? That's what I'm trying to make the program do.

A while loop might be useful. Something like this:
(this is not actual code to paste into your program, it's pseudo-code, or "idea" code, to serve as a springboard, only.

Code:
```/* I like to convert change_due to all pennies, right here, before this while
loop. That makes it all "homogeneous". (Next week, we'll study Pasteurizing
it, eh? :)  )
*/

tener = fiver = one = quarter = dime = nickel = penny = 0;
while(change_due > 0)   {

if(change_due >= 1000)   {
tener++;
change_due -= 1000;
continue;  //key part of the logic, starts the whole loop, all over again.
}

if(change_due >= 500)   {
fiver++;
change_due -= 500;
continue;
}
//continue for all denominations of your change, always going biggest to smallest values

} //end of while loop```
There are shorter ways of doing this using mod division, but this while loop is
instructive, as well.

To print it out, two print statements.
Code:
```   printf("\$10.00 &#37;d \n\$5.00 %d \n\$1.00 %d", tener, fiver, one);
//repeat this form of print statement, for the coins change.```

A while loop might be useful. Something like this:
(this is not actual code to paste into your program, it's pseudo-code, or "idea" code, to serve as a springboard, only.

Code:
```/* I like to convert change_due to all pennies, right here, before this while
loop. That makes it all "homogeneous". (Next week, we'll study Pasteurizing
it, eh? :)  )
*/

tener = fiver = one = quarter = dime = nickel = penny = 0;
while(change_due > 0)   {

if(change_due >= 1000)   {
tener++;
change_due -= 1000;
continue;  //key part of the logic, starts the whole loop, all over again.
}

if(change_due >= 500)   {
fiver++;
change_due -= 500;
continue;
}
//continue for all denominations of your change, always going biggest to smallest values

} //end of while loop```
There are shorter ways of doing this using mod division, but this while loop is
instructive, as well.

To print it out, two print statements.
Code:
```   printf("\$10.00 %d \n\$5.00 %d \n\$1.00 %d", tener, fiver, one);
//repeat this form of print statement, for the coins change.```
Thanks for the examples. I was able to do it with the while statement too, so I just kept it that way. I had the same exact idea in my head from looking at your examples, but I was missing the increment. That's why I was a bit confused. Either way, thanks for the help.