/* * CHANGE -- figure out how many quarters, dimes, nickels, pennies * are in some amount of cash * * Usage: change * * Inputs: User enters an integer for change * Outputs: number of quarters, dimes, nickels, pennies in the change * Exit Code: EXIT_SUCCESS (0) if valid integer entered * EXIT_FAILURE (1) otherwise * WARNING: Input must begin with an integer, but is not checked beyond that * * written for ECS 036A, Fall 2019 * * Matt Bishop, UC Davis * * 09/20/2019: original program written * * 10/03/2019: add loop to handle non-numeric inputs */ #include #include int main(void) { int ch; /* used to clear bogus line */ int amount; /* how much change the user has */ int leftover; /* used to hold amount as it decreases */ int numquarters; /* how many quarters */ int numdimes; /* how many dimes */ int numnickels; /* how many nickels */ int numpennies; /* how many pennies */ /* prompt the user for input */ printf("Amount of change to convert to coins: "); /* read in an amount; loop until you get it */ while (scanf("%d", &amount) != 1){ /* oops ... bad input */ printf("Amount must be nonnegative integer\n"); /* now clear the rest of the line */ while ((ch = getchar()) != EOF && ch != '\n') /* do nothing */ ; /* exit at EOF */ if (ch == EOF) return(EXIT_FAILURE); /* otherwise reprompt */ printf("Amount of change to convert to coins: "); } if (amount < 0){ printf("A NONnegative number, please!\n"); return(EXIT_FAILURE); } leftover = amount; /* computer the number of quarters and how much is left over */ numquarters = leftover / 25; leftover = leftover % 25; /* computer the number of dimes and how much is left over */ numdimes = leftover / 10; leftover = leftover % 10; /* computer the number of nickels and how much is left over */ numnickels = leftover / 5; leftover = leftover % 5; /* whatever is left over is the number of pennies */ numpennies = leftover; /* print the result */ printf("%d cents is %d quarters, %d dimes, %d nickels, and %d pennies\n", amount, numquarters, numdimes, numnickels, numpennies); /* thank you and goodnight! */ return(EXIT_SUCCESS); }