Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need some help with a program I need to write. I am a sophomore in high school taking AP Computer Science A. I need to make a program that computes change for any value (41.12 = 2 $20, 1 $1, 1 dime, 2 pennies) Any help would be appreciated

Here's one way that's easy for starters:
Create counters for each denomination.
Initialize them to 0.
Input the Total.
Start at the highest denomination.
As long as the total is bigger, increment the denomination counter, and subtract that amount from the total.
Move on to the next denomination.
It's also easier if you use integers instead of reals. In which case you actually work with pennies instead of dollars. $41.12 would be represented as 4112.
Here's some example stuff.
int TwentyDollarBillCount = 0;
int TenDollarBillCount = 0;
int FiveDollarBillCount = 0;
...
int NickelCount = 0;
int PennyCount = 0;Read in the Total using scanf, cin, or whatever.
OK. Here's where you calculate the counters:
while (Total >= 2000)
{
++TwentyDollarBillCount;
Total -= 2000;
}
while (Total >= 1000)
{
++TenDollarBillCount;
Total -= 1000;
}
...
while (Total >= 5)
{
++NickelCount;
Total -= 5;
}
PennyCount = Total;Now you have all the counters, and all you have to do is print them out. You can use cout or printf or whatever. You don't want to print the zero counters, so you need a lot of if statements:
if (TwentyDollarBillCount > 0)
printf("%d Twenty dollar bills ", TwentyDollarBillCount);if (TenDollarBillCount > 0)
printf(("%d Ten Dolllar Bills ", TenDollarBillCount);...
if (NickelCount > 0)
printf("%d Nickels ", NickelCount);if (PennyCount > 0)
printf("%d Pennies ", PennyCount);
If you want to get even more nit picky, you can check for counts of one, and futz with the plurals. It does look kinda lame to print "1 pennies" instead of "1 penny".

![]() |
scripting
|
Free n best way to learn
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |