# Thread: [Help] Change to give back program

1. Member Join Date
Feb 2010
Posts
2
Rep Power
0

## [Help] Change to give back program

Hi, i'm trying to complete a few excercies from my "Learn Java by Dietel" textbook, and they didn't post the answers to some of them so i was wondering if you guys could help me out. The program asks the user for the number to input and then it gives the amount of change back. Here is a example of what should be outputted:

Enter the price of the purchase in cents (\$1 or less): 33
The change from \$1.00 is: \$0.67
Number of quarters is: 2
Number of dimes is: 1
Number of nickels is: 1
Number of pennies is: 2

So the user enters the number 33 and the program delivers the rest of the information. Anyone have any idea how to write the program to do this? :confused:

Thanks  Reply With Quote

2. ## well, finding the change from a dollar is easy right, take the input and subtract it from 100, the remainder is the change.

To break it down into each piece of currency, in this case how the sample is printed, start from largest and word towards smallest. divide the change by the value of the coin piece.

In Java, using an integer to divide with for numerators and denominators gives an integer result, or truncating the remainder to get the largest whole number.

for example.

Java Code:
```int purchase = 33;
int change = 100- 33; // = 67

int quarters = change / 25;  // here 67 / 25 = 2```
and so on.  Reply With Quote

3. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
19

## One way to start is by examining the question and asking "Why is the example output 2 quarters, 1 dime etc?"

In other words, how would a human change giver figure out what change to give? Once you are able to describe, comprehensively and step-by-step, the process you yourself would take when confronted with someone who wants change for 33c from a dollar, then you will be in a position to start writing some code. The program is less an oracle that "delivers information" and more of a machine to carry out the algorithm that you have described.

to slow ...;(

start from largest and word towards smallest
In case, having been given the algorithm, there is nothing more that is interesting in the problem, you might want to consider why this works. That is why it leads to a minimum number of coins being given as change. (I take it this is an unstated condition and that you can't just offer 67 pennies as change and be done with it.)
Last edited by pbrockway2; 02-06-2010 at 12:57 AM.  Reply With Quote

4. Member Join Date
Feb 2010
Posts
2
Rep Power
0

## Thats what i thought as well, but then some things came to mind. For example 67 / 25 is 2.68. Wouldn't the program output 2.68 instead of 2? Aswell, each time something is divided the value changes, for example:

Java Code:
```int purchase = 33;
int change = 100- 33; // = 67

int quarters = change / 25;  // here 67 / 25 = 2
int dimes = change / 10;
int nickels = change / 5;
int pennies = change / 1;```
The dime, nickels, and pennies really need to be divided by the remaining amounts of 67 /25. Which would be 17, then the nickels would have to be divided by 7, and the pennies by 2. So basically what i'm trying to say is that every time a operation happens the value for "change" changes. How would you work around that?  Reply With Quote

5. Senior Member Join Date
Feb 2010
Posts
128
Rep Power
0

## Wouldn't the program output 2.68 instead of 2?
If int data type is used it will always show decimal digit. No real numbers.  Reply With Quote

6. Senior Member Join Date
Feb 2010
Location
Ljubljana, Slovenia
Posts
470
Rep Power
12

## And the line
Java Code:
`int quarters = change/25;`
doesn't alter the value of the variable change, it simply divides the number stored in the change variable with 25, and fixes that value to the quarters variable, leaving change unaltered.
Java Code:
`change = change/25;`
This would alter the change variable. And, if you think about it, the change variable should be altered (not by division of course), if you already gave 2 quarters back, the remaining total to be given back is decreased by 50 cents.  Reply With Quote

#### Posting Permissions

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