Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- #include <math.h>
- int main()
- {
- float change_as_float;
- do
- {
- change_as_float = get_float("Change owed: ");
- }
- while (change_as_float < 0);
- /* re-cast change due from float to an integer in pennies */
- int change = round(change_as_float * 100);
- int total_coins = 0;
- int coins[4] = {25, 10, 5, 1};
- /* if quarters can be used
- * see how many quarters can be used by using division and then rounding the result
- * return the remainder using the modulo (%) operation
- * continue through the denominations one by one from highest value to lowest value */
- for (int i = 0; i < 4; i++) {
- total_coins += round(change/coins[i]);
- change = change % coins[i];
- }
- printf("%i\n", total_coins);
- }
Add Comment
Please, Sign In to add comment