Algorithm Binomial(n, k)
for i ← 0 to n do // fill out the table row wise
for i = 0 to min(i, k) do
if j==0 or j==i then C[i, j] ← 1 // IC
else C[i, j] ← C[i-1, j-1] + C[i-1, j] // recursive relation
return C[n, k]
C program:
void main() {
int i, j, n, k, min, c[20][20]={0};
printf("\n Enter the value of n: ");
scanf("%d", &n);
printf("\n Enter the value of k: ");
scanf("%d", &k);
if(n >= k) {
for(i=0; i<=n; i++) {
// min = i<k? i:k;
for(j = 0; j <=i; j++) {
if(j==0 || j == i) {
c[i][j] = 1;
} else {
c[i][j] = c[i-1][j-1] + c[i-1][j];
}
}
}
printf("\t");
for(i=0; i<=n; i++) {
printf("%d\t",i);
}
printf("\n");
for(i=0; i<=n; i++) {
printf("%d\t",i);
// min = i<k? i:k;
for(j = 0; j <=n; j++) {
// if(j==0 || j == i)
printf("%d\t",c[i][j]);
}
printf("\n");
} printf("%dCr%d=%d",n,k,c[n][k]);
printf("\n");
} else {
printf("\n Invalid input \n Enter value n>=k \n");
}
}
Output:
Enter the value of n: 5
Enter the value of k: 3
0 1 2 3 4 5
0 1 0 0 0 0 0
1 1 1 0 0 0 0
2 1 2 1 0 0 0
3 1 3 3 1 0 0
4 1 4 6 4 1 0
5 1 5 10 10 5 1
5Cr3=10
0 comments:
Post a Comment