×

Binomial Coefficient Program in C

What is Binomial coefficient?

In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k is given as:

Binomial Coefficient Program in C

Here n>=k.

Example

Now let's see an example of how this formula works:

Input: B(9,2)

Output:

Binomial Coefficient Program in C

Ways to find Binomial Coeffiecient

There are three ways to find the Binomial coefficient:

  • Recursive formula
  • Multiplicative formula
  • Factorial formula

Now let's use the multiplicative formula to find the binomial coefficient:

#include<stdio.h>


void main() {
    int i, j, n, k, min, c[20][20]={0};
    printf("This program will help us to find the binomial Coefficient" );
    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 <= min; j++) {
                 if(j==0 || j == i) {
                     c[i][j] = 1;
                 } else {
                     c[i][j] = c[i-1][j-1] + c[i-1][j];
                 }
             }
         }
         printf("%d\t",c[n][k]);
         printf("\n");
     } else {
         printf("\n Invalid input \n Enter value n>=k \n");
     }
}

Output:

Binomial Coefficient Program in C

Explanation;

The above code is written to find the binomial coefficient of the given value. In the above code, we have taken input from the user using the scanf() function and stored the input into variables n and k. Then we compared both the inputs as we know the condition for finding the binomial coefficient: n should be greater than k. Then the min variable we have declared as an int data type is used to store the minimum value among the two variables, i.e., n and k. Using the for loop, we have to change the value of the 2d array, which we have declared as an integer type. Using the printf statement, we have printed the value in the 2d array.

Using Recursion:

Now let's see how to find the value of the binomial coefficient using the recursion.

Recursion is the process in which the function will call itself.

Code:

#include<stdio.h>
int BC(int n, int k);


int main()
{
        int n,k;
        printf("Enter the values of n and k such that n<k\n");
        printf("Enter the value of n: ");
        scanf("%d",&n);
        printf("Enter the value ok k: ");
        scanf("%d",&k);
        printf("\nBinomial coefficient\n",BC(n,k));
        printf("%d\n",BC(n,k));


        return 0;
}


int BC(int n, int k)
{
        if(k==0 || k==n)
                return 1;
        return BC(n-1,k-1) + BC(n-1,k);
}

Output:

Binomial Coefficient Program in C

Explanation:

The above code is written to get the binomial coefficient using the recursion. In the above code, we have created a function BC which will return the value of the binomial coefficient. In the main function, we have taken the input from the user such that the n value should be greater than k then we have called the function BC. In the function, k value is checked if the value is equal to zero or equal to n, then 1 will be returned. The BC function will be again called until the values are equal to n or zero. Afterwards, the backtracking process takes place, and the binary coefficient value will be returned. We can see that the values of the traditional and mathematical methods are the same.

Using Factorial

Now, let's see how to calculate the binomial coefficient using the factorial formula

Code:

#include <stdio.h>


int fak(int n) {
    int fak_n=1;
    for(int i=2; i<=n; i++){
        fak_n=fak_n*i;
    }
    return fak_n;
}


int bin(int n, int k) {
    return fak(n) / (fak(k) * fak(n - k));
}
        
int main() {
    int n;
    int k;
    scanf("%d%d", &n, &k);
    printf("%d\n", bin(n, k));
    return 0;
}

Output:

Binomial Coefficient Program in C

Explanation

The above code is written to print the binomial coefficient. This is the traditional method to find the binomial coefficient. In the above code, we have created a function fak that will return the numbers' factorial. We have created another function, bin() which will call the fak function in the bin function. We have given the formula for finding the binomial coefficient. In the main function, we have taken the values from the user. Then we called the bin function by passing the values. We have called the fak function in the bin function by passing the values using the formula. The fak will return the factorial value.


Related Topics

Character Class Tests in C

Introduction The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple...

4 minutes read.

Merge Sort in C

The merge sort follows the principle of the divide and conquers algorithm. Firstly it divides the input of an array into two halves and then calls itself for the two...

4 minutes read.

C and C++ Binary Files

What is a binary file? A file in which the content is written in binary format is called a binary file. A binary file is not a text file. There are...

7 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Multiplication table program in C using For loop

Before we move on the program of multiplication table of any natural number, first we have to know about the For loop statement. The syntax of ‘for’ loop in C programming...

3 minutes read.

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Find Union and Intersection of Two Arrays in C

Union You can find the union of the two sorted arrays using the join merge method on arr1[] and arr2[]. Use two index variables i and j with initial values i =...

4 minutes read.

Bubble sort in C

Bubble sort in C Bubble sort is defined as the algorithm that is used for the sorting mechanism. It follows the technique of replacing the first index with the more minor...

4 minutes read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

Assert() Function in C

Assert (): In C, the statements are executed with the exit statement. In C language declare, and it tests the condition parameters. If the statement executed will be false, it shows...

4 minutes read.

How to install a C compiler in windows 10-64bit

Many C compilers can be installed on your pc. The most downloaded compilers include GCC, Turbo C++, Visual Studio, etc. This article includes the installation of the Turbo C++ compiler in...

6 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

C program to compare the two strings

C program to compare the two strings Strings can be compared either by using the string function or without using string function. First, we will look at how we can compare the strings with...

4 minutes read.

Int in C

The keyword int in C programming stands for integer and it is a data type which is used for variable declarations or declaration of functions of different types. Similar to...

4 minutes read.

How to move a text in C

A text can be moved to the desired location in the output based on the coordinates provided in the gotoxy() function. What is gotoxy() function? In C language, gotoxy() is a pre-defined...

3 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.