Here's my implementation of the algorithm:
initially declared the variables as, quarter = 0.25, dime = 0.10, nickel = 0.05, penny = 0.01
takes user input for the current balance
iteration while (balance - quarter >= 0)
subtract quarter from the balance
increase coin by one
iteration while (balance - dime >= 0)
subtract dime from the balance
increase coin by one
iteration while (balance - nickel >= 0)
subtract nickel from the balance
increase coin by one
iteration while (balance - penny >= 0)
subtract penny from the balance
increase coin by one
print the number of coins
Now the problem is that my answer is always a few coins less. I tried to debug it by placing various print statements and it turns out that my program doesn't enter the last while loop.
For example when I enter balance = 0.41, the first loop subtracts the amount 0.25, the second loop subtracts 0.10, the third loop subtracts 0.05 and at this point the balance is 0.01 which is greater than 0 but it doesn't enter the last loop. I tried manually subtracting penny from the balance 0.01 to get -0.0000.
Can anyone tell me what I'm doing wrong here? If this was python, it would have handled this issue on it's own.