Advertisement
Guest User

cash program

a guest
Jul 25th, 2023
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3.  
  4. int get_cents(void);
  5. int calculate_quarters(int cents);
  6. int calculate_dimes(int cents);
  7. int calculate_nickels(int cents);
  8. int calculate_pennies(int cents);
  9. int main(void)
  10. {
  11.     // Ask how many cents the customer is owed
  12.     int cents = get_cents();
  13.  
  14.     // Calculate the number of quarters to give the customer
  15.     int quarters = calculate_quarters(cents);
  16.     cents = cents - quarters * 25;
  17.  
  18.     // Calculate the number of dimes to give the customer
  19.     int dimes = calculate_dimes(cents);
  20.     cents = cents - dimes * 10;
  21.  
  22.     // Calculate the number of nickels to give the customer
  23.     int nickels = calculate_nickels(cents);
  24.     cents = cents - nickels * 5;
  25.  
  26.     // Calculate the number of pennies to give the customer
  27.     int pennies = calculate_pennies(cents);
  28.     cents = cents - pennies * 1;
  29.  
  30.     // Sum coins
  31.     // int coins = quarters + dimes + nickels + pennies;
  32.  
  33.     // Print total number of coins to give the customer
  34.     printf("the change due is \n %i quarters \n %i dimes \n %i nickels \n %i pennies\n",
  35.         quarters, dimes, nickels, pennies);
  36. }
  37.  
  38. int get_cents(void)
  39. {
  40.     int cents;
  41.     do
  42.     {
  43.     cents = get_int("how many cents owed?\n");
  44.     }
  45.     while (cents >100 || cents <2);
  46.  
  47.     return cents;
  48.  
  49. }
  50.  
  51. int calculate_quarters(int cents)
  52. {
  53.     //i feel like this was probalby the intended solutions but is completely useless
  54.     // if (cents >= 75)
  55.     //     {
  56.     //         quarters = 3;
  57.     //     }
  58.     // else if (cents <= 74 && cents >= 50)
  59.     //     {
  60.     //         quarters = 2;
  61.     //     }
  62.     // else if (cents <= 49 && cents >= 25)
  63.     //     {
  64.     //         quarters = 1;
  65.     //     }
  66.     // else
  67.     //     {
  68.     //         quarters = 0;
  69.     //     }
  70.  
  71.     return cents / 25;
  72. }
  73.  
  74. int calculate_dimes(int cents)
  75. {
  76.     // TODO
  77.     return cents / 10;
  78. }
  79.  
  80. int calculate_nickels(int cents)
  81. {
  82.     // TODO
  83.     return cents / 5;
  84. }
  85.  
  86. int calculate_pennies(int cents)
  87. {
  88.     // TODO
  89.     return cents / 1;
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement