Skip to content

Instantly share code, notes, and snippets.

@ppgallas
Last active September 20, 2018 19:30
Show Gist options
  • Save ppgallas/d906418986ecc6746ba130485e1c81f3 to your computer and use it in GitHub Desktop.
Save ppgallas/d906418986ecc6746ba130485e1c81f3 to your computer and use it in GitHub Desktop.
A program which counts the smallest amount of coins to give out change in the store.
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
float cash;
int change;
int amountOfCoins; //counting coins
int coins[] = {25, 10, 5, 1};
int i = 0;
do
{
cash = get_float("How much money in $ do you have?\n"); //asks user for amount of money he has
}
while (cash <= 0);
change = roundf(cash * 100);
amountOfCoins = 0;
while (change > 0)
{
if (change >= coins[i])
{
change -= coins[i];
amountOfCoins++;
}
if (change < coins[i])
{
i++;
}
}
printf("Coins: %i\n", amountOfCoins);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment