Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- // This problem set is so much more confusing when you're not American
- // or British and can't remember what quarters/nickels/dimes/pennies are...
- // --------------------
- // ---- PROTOTYPES ----
- // --------------------
- int get_change(void);
- int quarters_calc(int change);
- int nickels_calc(int change);
- int dimes_calc(int change);
- int pennies_calc(int change);
- // -----------------------
- // ---- MAIN FUNCTION ----
- // -----------------------
- int main(void)
- {
- int change = get_change();
- int quarters = quarters_calc(change);
- change = change - quarters * 25;
- int dimes = dimes_calc(change);
- change = change - dimes * 10;
- int nickels = nickels_calc(change);
- change = change - nickels * 5;
- int pennies = pennies_calc(change);
- change = change - pennies;
- int coins = quarters + nickels + dimes + pennies;
- printf("%i\n", coins);
- }
- // --------------------------
- // ---- CUSTOM FUNCTIONS ----
- // --------------------------
- // Asks the user how much change he needs
- int get_change(void)
- {
- int change;
- do
- {
- change = get_int("Change owed: ");
- }
- while (change < 0);
- return change;
- }
- // Calculates the number of quarters (25c) needed
- int quarters_calc(int change)
- {
- int quarters = 0;
- while (change >= 25)
- {
- change = change - 25;
- quarters++;
- }
- return quarters;
- }
- // Calculates the number of dimes (10c) needed
- int dimes_calc(int change)
- {
- int dimes = 0;
- while (change >= 10)
- {
- change = change - 10;
- dimes++;
- }
- return dimes;
- }
- // Calculates the number of nickels (5c) needed
- int nickels_calc(int change)
- {
- int nickels = 0;
- while (change >= 5)
- {
- change = change - 5;
- nickels++;
- }
- return nickels;
- }
- // Calculates the number of pennies (1c) needed
- int pennies_calc(int change)
- {
- int pennies = 0;
- while (change >= 1)
- {
- change = change - 1;
- pennies++;
- }
- return pennies;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement