Created
March 24, 2021 17:50
-
-
Save edieblu/cefd2c2e23fb03cecfc1f05ebd8ff769 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <cs50.h> | |
#include <math.h> | |
// quarters (25¢), dimes (10¢), nickels (5¢), and pennies (1¢) | |
int main(void) | |
{ | |
int quarter = 25; | |
int dime = 10; | |
int nickel = 5; | |
int penny = 1; | |
float dollars = 0; | |
int coins = 0; | |
do | |
{ | |
dollars = get_float("Change owed:\n"); | |
} | |
while (dollars < 0); | |
// convert dollars to int to avoid imprecision | |
int cents = round(dollars * 100); | |
while (cents >= quarter) | |
{ | |
cents = cents - quarter; | |
coins++; | |
} | |
while (cents >= dime) | |
{ | |
cents = cents - dime; | |
coins++; | |
} | |
while (cents >= nickel) | |
{ | |
cents = cents - nickel; | |
coins++; | |
} | |
while (cents >= penny) | |
{ | |
cents = cents - penny; | |
coins++; | |
} | |
// return the number of coins needed | |
printf("%i\n", coins); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment