Sunday, 17 December 2017

Making Change using Greedy design technique

Implement Program for Making Change using Greedy design technique.



Program:


#include<stdio.h>
#include<math.h>
main()
{
int c[]={100,50,20,10,5,1};
int k=0,s[20],n,j=0;
int sum=0,max=c[0],i=0;

printf("Enter the Number:\n");
scanf("%d",&n);

for(i=0;i<20;i++)
                        {
s[i]=0;
                        }

while(sum!=n)

for(i=0;i<6;i++)
                                    {
if((sum+c[i])<=n)
                                                {
sum=sum+c[i];           
s[j]=c[i];
j++;
i--;
                                                }
                                    }


printf("Solution is:");
s[j]=0;
for(i=0;s[i]!=0;i++)
                        {
if(s[i]==0)
break;
printf("%d ",s[i]);
                        }
printf("\nNo of coins selected: %d \n",j);

}

OUTPUT:


No comments:

Post a Comment