Skip to content

Instantly share code, notes, and snippets.

@Praful932
Created June 19, 2019 10:26
Show Gist options
  • Save Praful932/42ff3ec3dbac3804582707788dab7d39 to your computer and use it in GitHub Desktop.
Save Praful932/42ff3ec3dbac3804582707788dab7d39 to your computer and use it in GitHub Desktop.

Revisions

  1. Praful932 created this gist Jun 19, 2019.
    36 changes: 36 additions & 0 deletions cash.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    //greedy least no of coins for change
    #include<stdio.h>
    #include<cs50.h>
    #include<math.h>
    int main(void)
    {
    float n;
    int coin=0;
    do
    {
    n=get_float("Change owed: ");
    }while(n<0);
    n=round(n*100);
    while(n>=25)
    {
    n=round(n-25);
    coin++;
    }
    while(n>=10)
    {
    n=round(n-10);
    coin++;
    }
    while(n>=5)
    {
    n=round(n-5);
    coin++;
    }
    while(n>=1)
    {
    n=round(n-1);
    coin++;
    }
    printf("%d\n",coin);

    }