×

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

Header files in C

Header files in C In the C programming language, header files are present, which have an extension of ‘.h’, and it consists of macro definitions, declarations, and so on that are...

4 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

Entry Control Loop in C

Initially, an entry control loop verifies the termination condition at the entry point. After that, its control passes to the main body of the while or for loop if the...

3 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

strtok() and strtok_r() functions in C with examples

strtok() and strtok_r() functions in C with examples C offer various strtok() and strtok r() methods for a string to be separated by a certain delimiter. Dividing a string is a...

2 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Nested loop in C

Definition of Nested Loop A nested loop is generally used when we want to run a loop statement inside another loop statement. This kind of loop is also known as a...

3 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.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

Round Robin Scheduling in C

Round Robin Scheduling in C Round robin is a CPU (Central Processing Unit) scheduling algorithm designed to share the time systems. It is one of the simplest and easiest scheduling algorithms...

4 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

Conio.h in C

Header files are the source files with the extension of .h. In c language header files are referred to as the helping files and they contain definitions of various functions...

4 minutes read.

Java Array vs ArrayList

As we all know, arrays are linear data structures that allow you to add elements to them continuously in memory address space, whereas ArrayList is a Collection framework class. Despite...

4 minutes read.

Commenting in C

Commenting in C Commenting in the C language is used to give out the information about the lines of the code which are included. It is one of the things that...

3 minutes read.

Difference between while and for loop in C

While Loop The syntax that can be used for the ‘while’ loop in the C programming language is mentioned below: while (test expression or termination condition)  {   // the main body of the...

6 minutes read.

Array Example in C

An array is a collection of similar types of data elements arranged in such a way that any number of values can be assigned to it. It can store values that...

4 minutes read.

Formatted Input and output function in C

Formatted I/O functions display multiple outputs to the user by taking various inputs. All data types like int, float, char and double are supported by the formatted I/O function. In...

4 minutes read.

Stdio.h in C

Header files are used to make the programmer’s efforts a lot easier. In order to make the programming simple, there are a number of libraries which are included as predefined...

4 minutes read.

Branching Statements in C

What is branching in c? Branching gets its name from the fact that the computer can select which branch to follow.Programs written in the C language execute statements one after the...

6 minutes read.