Skip to content

Instantly share code, notes, and snippets.

@toshvelaga
Created July 25, 2018 22:58
Show Gist options
  • Save toshvelaga/0ec338ce551ff5266beaa956ecb4488a to your computer and use it in GitHub Desktop.
Save toshvelaga/0ec338ce551ff5266beaa956ecb4488a to your computer and use it in GitHub Desktop.
CS50 Greedy
/* CS50 solution. Feel free to check with any online compiler. To get credit from Harvard you may have to make a few changes.
hmu at s.velaga@uky.edu if you have any questions. */
#include <stdio.h>
int main()
{
int c;
int numQ=0, numD=0, numN=0, numP=0, numcoins=0;
printf("How much change do you need?\n");
scanf("%d", &c);
if (c >= 0 && c < 100)
{
printf("You need %d cents.\n", c);
while (c >= 25)
{
c = c - 25;
numQ++;
}
while (c >= 10)
{
numD++;
c = c - 10;
}
while (c >= 5)
{
numN++;
c = c - 5;
}
while (c >= 1)
{
numP++;
c = c - 1;
}
numcoins = numQ + numD + numN + numP;
printf("\nTotal number of coins = %d.\n\n", numcoins);
printf("You need %d Quarters, ", numQ);
printf("%d Dimes, ", numD);
printf("%d Nickles, ", numN);
printf("and %d Pennies.\n", numP);
}
else
{
printf("Please enter the amount of cents you need between 0 and 100");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment