# Thread: Question on purchase and change program

1. ## Question on purchase and change program

I'm trying to write this program that asks for the purchase amount, the payment amount and then outputs the change in the number of bills and coins. I think I'm missing an 'if' statement for determining the coins. Can someone point me in the right direction with this? Thanks. Here is the code:

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

int main()

{

double p,t;
int coins,bills,change;
int twenties,tens,fives,ones,quarters,dimes,nickels,pennies;

do

{

printf("\nEnter the purchase amount:");
scanf ("%lf",& p);

printf("\nEnter the payment amount:");
scanf ("%lf",& t);

change=p-t;

bills=(int) change;

twenties=bills/200;

bills=bills%200;

tens=bills/100;

bills=bills%100;

fives=bills/50;

bills=bills%50;

ones=bills/10;

bills=bills%10;

coins=(int)((change-bills)*100);

quarters=coins/25;

coins=coins%25;

dimes=coins/10;

coins=coins%10;

nickels=coins/5;

coins=coins%5;

pennies=coins;

printf("\nYour change will be %d twenties, %d tens, %d fives, %d ones, %d quarters, %d dimes, %d nickels and %d pennies.\n",twenties,tens,fives,ones,quarters,dimes,nickels,pennies);

printf("\nContinue? (Y/N):");

}

return 0;

}```

2. change=p-t;
bills=(int) change;

You have declared change as an int, so the first statement already discards any decimal part on p-t, and the cast is redundant (since both are int). Is this what you want? Also, if I buy something that costs \$19 and I pay \$20 I expect \$1 of change, but the change variable here is -\$1; should it be t-p?

Hello, buffer overflow. See http://www.eskimo.com/~scs/C-faq/q12.20.html

3. That was the question I posted. I'm not sure how to get the change.

4. Code:
`scanf ("%s",&answer);`
if you want to check a character only then why not
Code:
```char answer='y';
Code:
`change=p-t;`
After this statement.You should check
Code:
`if(change<0).`
if it is then put a
Code:
`continue;`
but remember to initialise answer to 'y' at defination its itself

5. try this
Code:
```#include <stdio.h>

int main() {
double p,t,change;
int coins,bills;
int twenties,tens,fives,ones,quarters,dimes,nickels,pennies;

do {
printf("\nEnter the purchase amount:");
scanf ("%lf", & p);

printf("\nEnter the payment amount:");
scanf ("%lf", & t);

change = t - p;
printf("\nyour change is : %lf\n", change );
bills = (int)change;
coins=(int)((change-(double)bills)*100);

printf("that is %d \$ in bills and %d cents in coins\n", bills, coins );

twenties = bills/20;
bills -= twenties * 20;
tens = bills/10;
bills -= tens * 10;
fives = bills/5;
bills -= fives * 5;
ones=bills;

quarters = coins/25;
coins -= quarters*25;
dimes = coins/10;
coins -= dimes * 10;
nickels = coins/5;
coins -= nickels*5;
pennies = coins;

printf("\nYour change will be %d twenties, %d tens, %d fives, %d ones, %d quarters, %d dimes, %d nickels and %d pennies.\n",twenties,tens,fives,ones,quarters,dimes,nickels,pennies);

printf("\nContinue? (Y/N):");

return 0;
}```
Kurt

6. Thank you. One small issue. Now I get negative numbers in my printf statement at the end. If I put total as say 20.00 and payment at 12.00, I get "-1 fives and -3 ones." They are all set to %d (integers). How can I get rid of the negatives? Here is my latest code:

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

int main()

{

double p,t;
int coins,bills,change;
int twenties,tens,fives,ones,quarters,dimes,nickels,pennies;

do

{

printf("\nEnter the purchase amount:");
scanf ("%lf",& p);

printf("\nEnter the payment amount:");
scanf ("%lf",& t);

change=t-p;

bills=(int) change;

coins=(int)((change-(double)bills)*100);

twenties=bills/20;

bills=bills%20;

tens=bills/10;

bills=bills%10;

fives=bills/5;

bills=bills%5;

ones=bills/1;

bills=bills%1;

quarters=coins/25;

coins=coins%25;

dimes=coins/10;

coins=coins%10;

nickels=coins/5;

coins=coins%5;

pennies=coins;

printf("\nYour change will be %d twenties, %d tens, %d fives, %d ones, %d quarters, %d dimes, %d nickels and %d pennies.\n",twenties,tens,fives,ones,quarters,dimes,nickels,pennies);

printf("\nContinue? (Y/N):");

}

return 0;

}```

7. You can make your own absolute function.
absolute function is
if (x<0)
x=(-1)*x

8. You can do that one better by simply subtracting it from zero.
Code:
```if( x < 0 )
x = 0 - x;```

Quzah.

9. Ok...I got rid of the negatives but now I'm not getting the number of coins. For example, if I put 50.25 for purchase and 20.21 for payment, I get only the bills, 1 twenty and 1 ten but no change.

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

int main()

{

double p,t;
int coins,bills,change;
int twenties,tens,fives,ones,quarters,dimes,nickels,pennies;

do

{

printf("\nEnter the purchase amount:");
scanf ("%lf",& p);

printf("\nEnter the payment amount:");
scanf ("%lf",& t);

change=t-p;

if (change<0)

change=0-change;

bills=(int) change;

coins=(int)((change-(double)bills)*100);

twenties=bills/20;

bills=bills%20;

tens=bills/10;

bills=bills%10;

fives=bills/5;

bills=bills%5;

ones=bills/1;

bills=bills%1;

quarters=coins/25;

coins=coins%25;

dimes=coins/10;

coins=coins%10;

nickels=coins/5;

coins=coins%5;

pennies=coins;

printf("\nYour change will be %d twenties, %d tens, %d fives, %d ones, %d quarters, %d dimes, %d nickels and %d pennies.\n",twenties,tens,fives,ones,quarters,dimes,nickels,pennies);

printf("\nContinue? (Y/N):");

}