Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <stdio.h>
#include <cs50.h>
int main(void)
{
// prompt user for an amount of change
float user_input;
int change, num_of_coins;
do
{
printf("Enter the amount of change you want back: ");
user_input = get_float();
}
while(user_input < 0);
change = user_input * 100;
// keep track of coin used
num_of_coins = 0;
// always use the largest coin possible
while(change >= 25)
{
change = change - 25;
num_of_coins++;
}
while(change >= 10)
{
change = change - 10;
num_of_coins++;
}
while(change >= 5)
{
change = change - 5;
num_of_coins++;
}
while(change >= 1)
{
change = change - 1;
num_of_coins++;
}
// print the final number of coins
printf("You entered:\t%.2f\n", user_input);
printf("least coins possible:\t%d\n", num_of_coins);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.