Making Change Problem (Greedy Technique) in C

 


Code:

#include <stdio.h>
#include <stdlib.h>

void making_change(int ar[],int am);

int main()
{
    int n,clocks_per_sec;
    printf("Enter The Your amount: ");
    scanf("%d",&n);
    int arr[10]={2000,500,200,100,50,20,10,5,2,1};
    making_change(arr,n);
    return 0;
}
void making_change(int ar[],int am)
 {
     int change[am];
     int i,j,k;
     while(am!=0)
     {
         for(j=0;j<9;j++)
         {
             if(am >= ar[j])
             {
                 k++;
                 change[k]= ar[j];
                 am = am - ar[j];
                 break;
             }
         }
     }
     i=0;
     for(i=1;i<=k;i++)
     {
         if(i==k)
         {
             printf("%d ",change[i]);
         }
         else
         {
             printf("%d + ",change[i]);
         }
     }
 }

Output:




Post a Comment

Previous Post Next Post
Best Programming Books

Facebook

AJ Facebook
Checkout Our Facebook Page
AJ Blogs
Checkout Our Instagram Page