Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues
Created August 3, 2016 02:40
Show Gist options
  • Save CraigRodrigues/052a69b133a9c5af2cdf67b9d602c9f3 to your computer and use it in GitHub Desktop.
Save CraigRodrigues/052a69b133a9c5af2cdf67b9d602c9f3 to your computer and use it in GitHub Desktop.
CS50 Pset1 - Greedy
#include <stdio.h>
#include <cs50.h>
#include <math.h>
/** Write a program that first asks the user how much change is owed
and then spits out the minimum number of coins with which said change
can be made.*/
int main(void)
{
float amount;
int coins_returned;
//get non-negative float from the user
do
{
printf("How much change is owed?\n");
amount = GetFloat();
} while (amount < 0);
//convert the float into cents rounded
int cents = round(amount * 100);
//quarters
coins_returned = cents/25;
cents %= 25;
//dimes
coins_returned += cents/10;
cents %= 10;
//nickels
coins_returned += cents/5;
cents %= 5;
//pennies
coins_returned += cents;
printf("%i\n", coins_returned);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment