Skip to content

Instantly share code, notes, and snippets.

@aditiohri
Created May 9, 2020 20:16
Show Gist options
  • Save aditiohri/3e7e11c4e5bccb8094c1497bbc4df8ed to your computer and use it in GitHub Desktop.
Save aditiohri/3e7e11c4e5bccb8094c1497bbc4df8ed to your computer and use it in GitHub Desktop.
#include <cs50.h>
#include <stdio.h>
#include <math.h>
int main(void)
{
//create a variable to hold value of coins
int coins = 0;
//get change amount
float change = get_float("Change owed: ");
//if change is greater than 0
if (change > 0)
{
//convert amount to cents
int cents = round(change * 100);
do
{
// if cents are greater than or equal to denomination
if (cents >= 25)
{
// subtract the denominational value
cents -= 25;
// add 1 to coins
coins++;
}
else if (cents >= 10)
{
cents -= 10;
coins++;
}
else if (cents >= 5)
{
cents -= 5;
coins++;
}
else if (cents >= 1)
{
cents -= 1;
coins++;
}
}
while (cents >= 1);
printf("%i\n", coins);
}
// if change is a negative number or not a float
// call the function again
else if (!change || change < 0)
{
main();
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment