Skip to content

Instantly share code, notes, and snippets.

@sameekapdi
Created November 8, 2017 12:38
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 sameekapdi/e0ee149465e54210ebb284afab2d0b75 to your computer and use it in GitHub Desktop.
Save sameekapdi/e0ee149465e54210ebb284afab2d0b75 to your computer and use it in GitHub Desktop.
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
//declare variables
float change;
int coins, cents;
//Get user input, must be non-negative.
do
{
printf("O hai! How much change is owed?\n");
change = get_float();
}while (change <0);
//convert into int and minimise floating point imprecision
cents = (int)round(change*100);
coins = 0;
//Get how many 25c coins are needed, get the remainder via modulo
coins += cents / 25;
cents %=25;
//Get how many 10c coins are needed, get the remainder via modulo
coins+= cents / 10;
cents %= 10;
//Get how many 5c coins are needed, get the remainder via modulo
coins += cents / 5;
cents %= 5;
//remainder coins are pennies
coins += cents;
//Print result
printf("%i\n", coins);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment