Advertisement
Guest User

Untitled

a guest
Jan 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <cs50.h>
  3. #include <math.h>
  4.  
  5. int main(void){
  6. float input_amount;
  7. int pennies;
  8. int coins = 0;
  9.  
  10. // Get the user input
  11. do {
  12. printf("Enter amount of change due: ");
  13. input_amount = get_float();
  14.  
  15. if (input_amount < 0) {
  16. printf("You must enter a non-negative number. Try again.\n");
  17. }
  18. }
  19. while (input_amount < 0);
  20.  
  21. // Get total amount of pennies input is equal to (multiply float by 100. Round as floats are imprecise (420 = 419.9999...)
  22. pennies = round(input_amount * 100);
  23.  
  24. // Amount of 50p's that go into amount of pennies
  25. coins += pennies / 50;
  26. pennies %= 50;
  27.  
  28. // Amount of 20p's that go into amount of pennies
  29. coins += pennies / 20;
  30. pennies %= 20;
  31.  
  32. // Amount of 10p's that go into amount of pennies
  33. coins += pennies / 10;
  34. pennies %= 10;
  35.  
  36. // Amount of 5p's that go into amount of pennies
  37. coins += pennies / 5;
  38. pennies %= 5;
  39.  
  40. // Amount of 2's that go into amount of pennies
  41. coins += pennies / 2;
  42. pennies %= 2;
  43.  
  44. // Amount of 1p's that go into amount of pennies
  45. coins += pennies / 1;
  46. pennies %= 1;
  47.  
  48. printf("Minimum amount of coins required: %i\n", coins);
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement