# Thread: Vending Machine Change - LOGIC Understanding ISSUE

1. Member
Join Date
Jan 2012
Posts
40
Rep Power
0

## Vending Machine Change - LOGIC Understanding ISSUE

NEWBIE JAVA:

I have a simple java program to write which tells the user combination of coins that equals to the amount of change i.e:

user input 87
output:
3 quarters
1 dime
0 nickels
2 pennies

SIMPLE RIGHT?! - we'll my book shows the example BUT my confusion is how the program remembers the remainder which is passed to the next column of let say dime

i.e

originalAmount = amount;
quarters = amount /25;
amount = amount % 25; <---- this is confusing for me?!?! how can the integer = integer % 25
dimes = amount / 10; <--- HOW THE PROGRAM remembers the "remainder" instead of the original user input as the code it self tells you dimes = amount where "amount" is what user input NOT remainder. PLease help.
amount = amount % 10;
so on ....;

What I don't understand is HOW this algorithm works. I mean we have
int amount where user inputs the number we get the first calculation
amount/25 = how many quarters and then amount %25 WILL tell us about
the reminder. By looking at this piece of code I would say that the
system should start the calculation for the dimes again from the
original number since the code says dimes = amount/10 AND amount =
amount%10. My understanding is that the calculation should be done
from the original user input.

Book or code it self is not clear for me how the reminder is
"REMEMBERED" and then pass on to the next calculation>!?!?

UNLESS the code: amount=amount%25 gets the remainder so the next code under it is REQUIRED to read from the last prompt code - Please advise if I think right?

dk

2. Member
Join Date
Jan 2014
Posts
9
Rep Power
0

## Re: Vending Machine Change - LOGIC Understanding ISSUE

let's say you insert amount of 90
quarters = amount/25
amount = amount % 25; <--- this part is where 'amount' on the left side gets assigned the remainder after the calculations on the right side are done..i.e ( 90%25= 15)
// amount = 15
dimes = amount/10 <-- now that amount =15 ... dimes would be equal to 1.
amount = amount%10 <--- this part is where 'amount' on the left side gets assigned the remainder after the calculations on the right side are done..i.e( 15%10 = 5)
//amount=5
etc.
Do you understand ?

3. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
5

## Re: Vending Machine Change - LOGIC Understanding ISSUE

the '=' operator assigns a value to a variable. You see in programming, it makes sense to say x = x +1, something that can't hold true.

amount starts as the original amount and changes throughout the execution. it stores the updated values to the amount variable that's how it 'remembers'

amount = amount % 25.. means amount becomes what is was modulo 25 and stays that way until changed.

4. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
5

## Re: Vending Machine Change - LOGIC Understanding ISSUE

lol, didn't realize this was a zombie because it had a fresh response...

5. ## Re: Vending Machine Change - LOGIC Understanding ISSUE

Originally Posted by superhaNds
lol, didn't realize this was a zombie because it had a fresh response...
Nevermind; it happens to all of us; you can take a shower around the corner while I close this dead, old thread.

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•