Skip to content

Instantly share code, notes, and snippets.

@sojohnnysaid
Created January 12, 2018 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sojohnnysaid/8c5affacc7b001e79408625f07dcd2df to your computer and use it in GitHub Desktop.
Save sojohnnysaid/8c5affacc7b001e79408625f07dcd2df to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
#include <cs50.h>
int main(void)
{
// prompt user for an amount of change
float user_input;
int change, num_of_coins;
do
{
printf("Enter the amount of change you want back: ");
user_input = get_float();
}
while(user_input < 0);
change = round(user_input*100);
// keep track of coin used
num_of_coins = 0;
// always use the largest coin possible
// modulo math version is tricky
num_of_coins += change / 25;
change %= 25;
num_of_coins += change / 10;
change %= 10;
num_of_coins += change / 5;
change %= 5;
num_of_coins += change / 1;
change %= 1;
// print the final number of coins
printf("You entered:\t%.2f\n", user_input);
printf("least coins possible:\t%d\n", num_of_coins);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment