Although I'm aware that there are other threads about this, I've created my own because I don't want to see others' solutions. All of the results print fine except for 4.2. I'm aware of floating point imprecision and have tried to solve it, but cannot do so.
Can someone point me in the right direction on this? (No solutions, just a nod in the right direction.)
#include<stdio.h>
#include<cs50.h>
#include<math.h>
int main(void)
{
double quarter = 0.250, dimes = 0.100, nickels = 0.050, pennies = 0.010;
double $change = 0.00;
int coins = 0;
double rem = 0.00;
double rem2 = 0;
double rem3 = 0;
double a = 0;
int b = 0;
double c = 0;
double d = 0;
do
{
$change = get_float("Change owed: ");
}
while( $change < 0);
int cents = round( $change * 100);
a = $change / quarter;
rem = fmod($change, quarter);
b = rem / dimes;
rem2 = fmod(rem, dimes);
c = rem2 / nickels;
rem3 = fmod(rem2, nickels);
d = rem3 / pennies;
coins = a + b + c + d;
printf("%i\n", coins);
return 0;
}