C program to Find the binomial coefficient using dynamic programming

#include<stdio.h>
#include<conio.h>
void main()
{
 int i,j,k,n,c[50][50];
 clrscr();
 printf("Enter the value of n and k : ");
 scanf("%d %d",&n,&k);
 for(i=0;i<=n;i++)
  for(j=0;j<=k;j++)
   c[i][j]=0;
 for(i=0;i<=n;i++)
 {
  c[i][0]=1;
  c[i][i]=1;
 }
 for(i=2;i<=n;i++)
  for(j=1;j<=i-1;j++)
   c[i][j]=c[i-1][j-1]+c[i-1][j];
 printf("\nThe table for valuation is\n");
 for(i=0;i<=n;i++)
 {
  for(j=0;j<=k;j++)
   if(c[i][j]!=0)
    printf("\t %d",c[i][j]);
  printf("\n");
 }
 printf("\nThe binomial coffient of c(%d,%d) is %d",n,k,c[n][k]);
 getch();
}

Output:


No comments:

Post a Comment