Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- int main (void)
- {
- float dollars = 0;
- // get input, if not valid; retry
- do
- {
- printf("Amount of change (in $): ");
- dollars = GetFloat();
- if (dollars <= 0)
- {
- printf("Must be positive, try again.\n");
- }
- } while (dollars <= 0);
- // turn dollars into cents, make quarters, dime etc. into integers
- int cents = (int) (dollars * 100);
- double round(double cents);
- int q = 25;
- int d = 10;
- int n = 5;
- int p = 1;
- //variable for counting number of coins for change
- int coins = 0;
- // if cents bigger than 25 then subtract 25 and so forth
- if (cents > q)
- {
- cents = (cents - q);
- coins++;
- }
- else if (cents > d)
- {
- cents = (cents - d);
- coins++;
- }
- else if (cents > n)
- {
- cents = (cents - n);
- coins++;
- }
- else
- {
- cents = (cents - p);
- coins++;
- printf("Amount of coins back: %i\n", coins);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement