# Thread: Help figuring out why calculations are not working correctly

1. ## Help figuring out why calculations are not working correctly

Write a program that will calculate the types of coins that will add up to a Dollar cents number that is input by the user. For example if the user inputs 1.50, the program should output “6 quarters are equal to \$1.50”. Take into account, quarters, dimes, nickels and pennies. As another example if the user inputs 2.12, the program should ouput “8 quarters, 1 dime and 2 pennies are equal to \$2.12”.

Your program should give the minimum number of coins that will add up to the total amount, i.e. Your output for \$1.50 should be 6 quarters and not 15 dimes or 30 nickels.

for some reason when i input certain values like .95 it doesn't set dimes to 2 but sets it to 1 instead. i can't figure out it not working correctly. PLZ help
Code:
```#include <stdio.h>
#include <math.h>

int main()
{
double money,money2;
int quarter=0,dime=0,nickle=0,cent=0;
printf("Input the ammount of money: ");
scanf("%lf",&money);

money2=money;
printf("%lf \n",money2);
if(money2/.25>=1)
{
money2=money2/.25;
quarter=(int) money2;
money2=money-quarter*.25;
printf("%lf \n",money2);

}
if(money2/.1>=1)
{
money2=money2/.1;
dime= money2;//******************
money2=money-quarter*.25-dime*.1;
printf("%lf \n",money2);
}
if(money2/.05>=1)
{
money2=money2/.05;
nickle=(int)money2;
money2=money-quarter*.25-dime*.1-nickle*.05;
printf("%lf \n",money2);
}
if(money2/.01>=1)
{
money2=money2/.01;
cent=(int)money2;
money2=money-quarter*.25-dime*.1-nickle*.05-cent*.01;
printf("%lf \n",money2);
}

printf("%d quarters\n",quarter);
printf("%d dimes\n",dime);
printf("%d nickles\n",nickle);
printf("%d cents\n",cent);
}```

2. When you deal with doubles and floats you get into the approximation problem. First thing I'd do is change >= 1 to >=.999. That should handle the approximation problem.

Try that real quickly, and see what that does.

Another way to do this is to round the double by adding 0.5 and then dividing by 1, and casting that to an int variable, before the comparison.

The way I like to deal with money is to immediately convert all monies to pennies, and then handle all calculations using integers. Final print out compensates for that conversion.

3. Rounding errors. There are certain values that a floating point type cannot represent exactly. 0.1 is one of them. You also need to account for whether rounding goes up or down.

You'd be better off converting values to integers and doing all your working with integer arithmetic (e.g. \$1.50 is 150 cents, a quarter is 25 cents rather than \$0.25). Mathematically the same, but not subject to things you don't understand about floating point.

4. didn't help at all. still have the same problem. here is a bad output. look at the cents.

Input the ammount of money: 1.49
1.490000
0.240000
0.040000
0.010000
5 quarters
2 dimes
0 nickles
3 cents
Press any key to continue . . .

5. I don't like your logic. You have a lot of division by a float, and then there's casting to an int, done too late (imo).

It's hodgey-podgey stuff. Clean it up, and simplify it. Convert to integer ONE time, right at the start, and never do it again.

The problem is, you're close - and so you want to keep going and get that last %1 correct. Close is the greatest enemy of right. If you were WAY wrong, you'd change your code in a second. But you're close, so...

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

int main()
{
double money;
int money2;

printf("Input the ammount of money: ");
scanf("%lf",&money);

money2= (int)( money * 100);
printf("%d cents\n\n",money2);

printf("%d quarters\n",money2 / 25);
money2 %= 25;
printf("%d dimes\n",money2 / 10);
money2 %= 10;
printf("%d nickles\n",money2 / 5);
money2 %= 5;
printf("%d cents\n",money2);
}```

7. Thank You very much

8. Originally Posted by koolbklyn
Thank You very much
LOL...Sometimes I just feel like showing off....

However; I will urge you to take the time to understand how it works.

That's a class assignment, your teacher will find it. So don't be handing it in.