Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@edieblu
Created March 24, 2021 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edieblu/cefd2c2e23fb03cecfc1f05ebd8ff769 to your computer and use it in GitHub Desktop.
Save edieblu/cefd2c2e23fb03cecfc1f05ebd8ff769 to your computer and use it in GitHub Desktop.
#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