#include <stdio.h> int main(void) { float n; do { n = get_float("Change owed: \n"); } while(n<0.0); int quarter=0, dime=0, nickel=0, penny=0; while(n>=0.25) { quarter++; n=n-0.25; } while(n>=0.10) { dime++; n=n-0.10; } while(n>=0.05) { nickel++; n=n-0.05; } while(n>=0.01) { penny++; n=n-0.01; } printf("%i\n", (quarter + dime + nickel + penny)); }

I have to calculate the least number of quarters, dimes, nickels and pennies which need to be handed out as change for a given amount.

For eg, 0.42$ will require a quarter, a dime, a nickel, and two pennies. The output will be 5.

My code works fine for quarters, dimes, and nickels but gives a value one less than required only in the case of pennies. Suppose I input 0.02$ the output is 1 whereas it should be 2.

The do-while loop asks the user for a value repeatedly until a positive value is entered.