Skip to content

Instantly share code, notes, and snippets.

@ThatOneCubanDude
Created March 26, 2020 22:51
Show Gist options
  • Save ThatOneCubanDude/c4a950587cf9e8f4f8eb4115224a02de to your computer and use it in GitHub Desktop.
Save ThatOneCubanDude/c4a950587cf9e8f4f8eb4115224a02de to your computer and use it in GitHub Desktop.
My solution to CS50x Problem Set 1 - Cash. Feel free to critique or ask questions.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
float get_positive_float(void);
int main(void)
{
//creates variable "change" (amount of cents due) and varable "coins" (the amount of coins to be given out)
float change = round(get_positive_float() * 100);
int coins = 0;
//main code starts to see how many quarters it can take out of the change due, then it moves on to dimes,
//then nickels, and finally pennies
for (coins = 0; change - 25 >= 0; change = change - 25)
{
coins++;
}
for (true; change - 10 >= 0; change = change - 10)
{
coins++;
}
for (true; change - 5 >= 0; change = change - 5)
{
coins++;
}
for (true; change - 1 >= 0; change = change - 1)
{
coins++;
}
//prints the least amount of coins with which you can give out change
printf("%i\n", coins);
}
//creates function "get_positive_float" that only accepts postive float values as input
float get_positive_float(void)
{
float n;
do
{
n = get_float("Change owed: ");
}
while (n < 0);
return n;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment