Advertisement
Kawsar_Hossain

Coin change

Jul 26th, 2020
1,438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int amount,n,i,j;
  6.     printf("Enter total amount: ");
  7.     scanf("%d", &amount);
  8.     printf("Enter number of different coins: ");
  9.     scanf("%d", &n);
  10.     int coin[n],count[n];
  11.     printf("Enter the coin values: ");
  12.     for(i=0;i<n;i++)
  13.     {
  14.         scanf("%d", &coin[i]);
  15.         count[i]=0;
  16.     }
  17.     int amount1=amount;
  18.     for(i=0;i<n;i++)
  19.     {
  20.         if(amount==0)
  21.         {
  22.             break;
  23.         }
  24.         if(amount>=coin[i])
  25.         {
  26.             count[i]=(amount/coin[i])+count[i];
  27.             amount=amount%coin[i];
  28.             for(j=i;j>=0;j--)
  29.             {
  30.                 if(amount==0)
  31.                 {
  32.                     break;
  33.                 }
  34.                 count[i]=(amount/coin[j])+count[i];
  35.                 amount=amount%coin[j];
  36.                
  37.             }
  38.             if(amount!=0 && count[i]>0)
  39.             {
  40.                 count[i]=0;
  41.             }
  42.         }
  43.         amount=amount1;
  44.        
  45.     }
  46.     int min=amount1+1;
  47.     for(i=0;i<n;i++)
  48.     {
  49.         if(count[i]<min && count[i]!=0)
  50.         {
  51.             min=count[i];
  52.         }
  53.     }
  54.     printf("Minimum number of coin: %d", min);
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement