Skip to content

Instantly share code, notes, and snippets.

@deeunix

deeunix/cash.c Secret

Created September 13, 2020 04:22
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 deeunix/4fc124971605de3437acf84456b03992 to your computer and use it in GitHub Desktop.
Save deeunix/4fc124971605de3437acf84456b03992 to your computer and use it in GitHub Desktop.
Collins cs50 Cash solution
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
int count = 0; //to count
float change; //declare a float variable "change"
do
{
change = get_float("Change owed: "); //get change from user
}
while (change < 0); //make sure change is not less than 0
int cent = round(change * 100); //round off to the nearest whole number
while (cent >= 25)
{
cent = cent - 25; // divide by 25 and give account of remainder
count++; //count number of times
}
while (cent >= 10)
{
cent = cent - 10; //divide by 10 and give account of remainder
count++; //count number of times
}
while (cent >= 5)
{
cent = cent - 5; //divide by 5 and give account of remainder
count++; //count number of times
}
while (cent >= 1)
{
cent = cent - 1; //divide by 1 and give account of remainder
count++; //count number of times
}
printf("%i\n", count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment