I've stuck with my code. Sometimes it counts correctly but most of the time - it doesn't. The below is a part of my code. I would appreciate if you point out what is wrong with this logic:
float change;
int quarter = 25;
int dime = 10;
int nickle = 5;
int penny = 1;
int leftover0;
int leftover1;
int leftover2;
int leftover3;
int coinq; //amount of quarters
int coind; //amount of dimes
int coinn; //amount of nickles
int coinp; //amount of pennies
int coins;
//round and convert to cents:
int cents;
cents = round(change * 100);
//use the largest coin:
while (quarter <= cents)
{
quarter++;
}
leftover0 = cents - quarter;
// coinq keeps track of how many coins of quarter was used:
coinq = (cents - leftover0) / 25;
while (dime <= leftover0)
{
dime++;
}
leftover1 = leftover0 - dime;
coind = (leftover0 - leftover1) / 10;
while (nickle <= leftover1)
{
nickle++;
}
leftover2 = leftover1 - nickle;
coinn = (leftover1 - leftover2) / 5;
while (penny <= leftover2)
{
penny++;
}
leftover3 = leftover2 - penny;
coinp = (leftover2 - leftover3) / 1;
coins = coinq + coind + coinn + coinp;
printf("%d\n", coins);