Skip to content

Instantly share code, notes, and snippets.

@kt-aakash
Created July 23, 2017 14:54
Show Gist options
  • Save kt-aakash/01695524ae96f5ffd5841065df89cd57 to your computer and use it in GitHub Desktop.
Save kt-aakash/01695524ae96f5ffd5841065df89cd57 to your computer and use it in GitHub Desktop.
cs50 pset1 greedy.c
#include<stdio.h>
#include<cs50.h>
int penny(int n, int temp)
{
if(n>=25){
n -= 25;
temp++;
}
else if(n>=10 && n<25){
n -= 10;
temp++;
}
else if(n>=5 && n<10){
n -= 5;
temp++;
}
else if(n>=1 && n<10){
n -= 1;
temp++;
}
if(n>0)
{
//printf("n=%d/n",n);
//printf("temp=%d/n",temp);
return(penny(n, temp));
}
return temp;
}
int main(void)
{
float change;
int n, count=0;
do{
printf("O hai! How much change is owed?\n");
change = get_float();
}while(change<0);
n = (int)(change*100)%100;
count = penny(n, count);
printf("%d\n",count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment