×

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

Add two numbers using the function in C

Here we will learn how to add two numbers by creating function in C. Let’s learn this with help of example. Code: - #include <stdio.h> int add_two_no(int a, int b); int main(){   int first_num,...

1 minute read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

Compound Interest Program in C

Interest on loans and deposits is compound interest. This is the most commonly used concept in everyday life. Compound interest on an amount is based on principal and interest earned...

3 minutes read.

What is the main in C?

In the C programming language, "main" is a predefined term or function. It is the primary function in every C program and is in charge of beginning and terminating the...

6 minutes read.

Pascal Triangle in C

The pascal triangle in c is an array of binomial coefficients in triangular form. Here the nth row contains the binomial coefficient of ncr.In a pascal triangle, every number is...

2 minutes read.

C –Structure

C structure is a collection of different types of data that is grouped together. It is used to represent records.Structkeyword is used to create a structure. Each element of a...

1 minute 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.

Difference between Scope and Lifetime in C

In this article, we will discuss about the key differences between scope and lifetime in C, including their definitions, applications, and how they relate to one another. By the end...

8 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

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

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

fork() in C

Introduction To create a new function in the system, there is a need for a system call, i.e. fork system call. It is also known as the child process. These child...

2 minutes read.

Run Time Polymorphism in C

What is polymorphism? Polymorphism refers to the existence of various forms. Polymorphism can be simply defined as a message's capacity to be presented in multiple forms. An individual who can have...

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.

Else If Ladder in C

What is Else If Ladder: When there are multiple options and a user has to decide from the options, we use Else If Ladder. Basically, it is an extension of if...

3 minutes read.

One Dimensional Array in C

One Dimensional Array in C: In the C programming language, an array is defined as a collection of one or more values of the same type. Every value present in...

4 minutes read.

Why C is a middle level language

The C programming language is generally referred to as a high level language but we glance through the backend of C, i.e the working of C as a programming language...

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

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.