Guest User

Untitled

a guest
Jan 21st, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <math.h>
  4.  
  5. int main()
  6. {
  7. float change_as_float;
  8. do
  9. {
  10. change_as_float = get_float("Change owed: ");
  11. }
  12. while (change_as_float < 0);
  13.  
  14. /* re-cast change due from float to an integer in pennies */
  15. int change = round(change_as_float * 100);
  16. int total_coins = 0;
  17. int coins[4] = {25, 10, 5, 1};
  18.  
  19. /* if quarters can be used
  20. * see how many quarters can be used by using division and then rounding the result
  21. * return the remainder using the modulo (%) operation
  22. * continue through the denominations one by one from highest value to lowest value */
  23. for (int i = 0; i < 4; i++) {
  24. total_coins += round(change/coins[i]);
  25. change = change % coins[i];
  26. }
  27.  
  28. printf("%i\n", total_coins);
  29. }
Add Comment
Please, Sign In to add comment