Program for Binomial Coefficients table

Given an integer max, print Binomial Coefficients table that prints all binomial coefficients B(m, x) where m and x vary from 0 to max

Example :

Input : max = 3
Output :  
 0   1
 1   1   1
 2   1   2   1
 3   1   3   3   1

The easiest way to explain what binomial coefficients are is to say that they count certain ways of grouping items. Specifically, the binomial coefficient B(m, x) counts the number of ways to form an unordered collection of k items chosen from a collection of n distinct items.
Binomial coefficients are used in the study of binomial distributions and multicomponent redundant systems. It is given by B(m, x)= m! / x!(m - x)!, m> = x

Example :

Compute B(7, 3)  where m = 7 and x = 1 
          (7!/3!(7-3)!)7
        = 7!/3!*4!
        = (7*6*5*4*3*2*1)/(3*2*1)*(4*3*2*1)
        = 35

A table of binomial coefficients is required to determine the binomial coefficient for any value m and x.

Problem Analysis :
The binomial coefficient can be recursively calculated as follows –
B(m, x) = B(m, x - 1) [m - x + 1 / x], x = 1, 2, 3 .... m  further, b(0, 0) = 1

That is the binomial coefficient is one when either x is zero or m is zero. The program prints the table of binomial coefficients for m = 10.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for binomial coefficients
#include <stdio.h>
  
// Function to print binomial table
int printbinomial(int max)
{
    for (int m = 0; m <= max; m++) {
        printf("%2d", m);
        int binom = 1;
        for (int x = 0; x <= m; x++) {
  
            // B(m, x) is 1 if either m or x is
            // is 0.
            if (m != 0 && x != 0)
  
                // Otherwise using recursive formula
                // B(m, x) = B(m, x - 1) * (m - x + 1) / x
                binom = binom * (m - x + 1) / x;
  
            printf("%4d", binom);
        }
        printf("\n");
    }
}
  
// Driver Function
int main()
{
    int max = 10;
    printbinomial(max);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for 
// binomial coefficients
import java.io.*;
  
class GFG 
{
      
// Function to print 
// binomial table
static void printbinomial(int max)
{
    for (int m = 0; m <= max; m++)
    {
        System.out.print(m + " ");
        int binom = 1;
        for (int x = 0; x <= m; x++) 
        {
  
            // B(m, x) is 1 if either 
            // m or x is is 0.
            if (m != 0 && x != 0)
  
                // Otherwise using 
                // recursive formula
                // B(m, x) = B(m, x - 1) * 
                //            (m - x + 1) / x
                binom = binom * (m - x + 1) / x;
  
            System.out.print(binom + " ");
        }
        System.out.println();
    }
}
  
// Driver Code
public static void main (String[] args) 
{
    int max = 10;
    printbinomial(max);
}
}
  
// This code is contributed
// by akt_mit

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for binomial
# coefficients
  
# Function to print binomial table
def printbinomial (max):
      
    for m in range(max + 1):
        print( '% 2d'% m, end = ' ')
        binom = 1
        for x in range(m + 1):
              
            # B(m, x) is 1 if either m 
            # or x is is 0.
            if m != 0 and x != 0:
                  
                # Otherwise using recursive
                # formula
                # B(m, x) = B(m, x - 1) *
                #           (m - x + 1) / x
                binom = binom * (m - x + 1) / x
            print( '% 4d'% binom, end = ' ')
        print("\n", end = '')
          
# Driver Function
max = 10
printbinomial(max)
  
# This code is contributed by "Sharad_bhardwaj".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for binomial coefficients
using System;
  
public class GFG {
  
    // Function to print binomial table
    static void printbinomial(int max)
    {
        for (int m = 0; m <= max; m++) {
            Console.Write(m + " ");
            int binom = 1;
            for (int x = 0; x <= m; x++) {
  
                // B(m, x) is 1 if either m
                // or x is is 0.
                if (m != 0 && x != 0)
  
                    // Otherwise using recursive formula
                    // B(m, x) = B(m, x - 1) * (m - x + 1) / x
                    binom = binom * (m - x + 1) / x;
  
                Console.Write(binom + " ");
            }
            Console.WriteLine();
        }
    }
  
    // Driver Function
    static public void Main()
    {
        int max = 10;
        printbinomial(max);
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program for 
// binomial coefficients
  
// Function to print
// binomial table
function printbinomial($max)
{
    for ($m = 0; $m <= $max; $m++)
    {
        echo $m;
        $binom = 1;
        for ($x = 0; $x <= $m; $x++) 
        {
  
            // B(m, x) is 1 if either 
            // m or x is 0.
            if ($m != 0 && $x != 0) 
              
                // Otherwise using 
                // recursive formula
                // B(m, x) = B(m, x - 1) * 
                //            (m - x + 1) / x
                $binom = $binom * ($m - $x + 1) / $x;
              
            echo " ", $binom, " ";
        }
    echo "\n"
    
}
  
// Driver Code
$max = 10;
printbinomial($max);
  
// This code is contributed by aj_36
?>

chevron_right



Output :

 0   1
 1   1   1
 2   1   2   1
 3   1   3   3   1
 4   1   4   6   4   1
 5   1   5  10  10   5   1
 6   1   6  15  20  15   6   1
 7   1   7  21  35  35  21   7   1
 8   1   8  28  56  70  56  28   8   1
 9   1   9  36  84 126 126  84  36   9   1
10   1  10  45 120 210 252 210 120  45  10   1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : jit_t