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:




AJ Blogs

Hello everyone, My name Arth and I like to write about what I learn. Follow My Website - https://sites.google.com/view/aj-blogs/home

Post a Comment

Previous Post Next Post
Best Programming Books

Facebook

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