Change Machine Code

I'm writing a code for a change machine, in which the user input the cost of their purchase, then the amount of cash that is given for the purchase, and then the amount of change is output. The part that I can't get is that along with the amount of change that is given back, it has to be broken down into how many dollars is given back, quarters, dimes, nickels, and pennies. I don't know how code it to specify how many of each is given back.

Thanks
Say you have $3.52 in change that needs to be returned. Easiest way to represent this is as an integer amount of the number of pennies in change (if you use float or double values you may get results like 3.000000001 for example).

So, start with you largest denomination and divide the change by that value. Assuming you want to deal with any amount of change, you'd start with the 20 dollar bill, which is equal to 2000 pennies. That divides into your change 0 times, so you move to the next lower denomination. 10 and 5 dollar bills also divide into it 0 times.

When you divide by one dollar, or 100 pennies, you get 3 dollar bills, you then subtract this value from your total change, which levaes 52 pennies, and continue on to a quarter (25 pennies) This divides into your leftover change twice, or 2 quarters. Continue on with dimes, and nickles, after which whatever is left is the number of pennies to give back in change.

So in that example, 3 dollars, 2 quarters, and 2 pennies.

Oh, and slightly off topic, when I first read the title of your post, I thought you wanted to know how to change 'machine code' ie executable code.
Last edited on
Topic archived. No new replies allowed.