Program for Binomial Coefficients table

10 May, 2022
Given an integer max, print Binomial Coefficients table that prints all binomial coefficients B(m, x) where m and x vary from 0 to maxExample :  
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 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 –   further, 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 . 
C++
Java
Python3
C#
PHP
Javascript
// C++ program for binomial coefficients#include <stdio.h> // Function to print binomial tableint 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 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 Functionint main(){    int max = 10;    printbinomial(max);    return 0;}
// Java program for// binomial coefficientsimport java.io.*; class GFG{     // Function to print// binomial tablestatic 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 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 Codepublic static void main (String[] args){    int max = 10;    printbinomial(max);}} // This code is contributed// by akt_mit
# Python3 program for binomial# coefficients # Function to print binomial tabledef 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 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 Functionmax = 10printbinomial(max) # This code is contributed by "Sharad_bhardwaj".
// C# program for binomial coefficientsusing 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 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.
<?php// PHP program for// binomial coefficients // Function to print// binomial tablefunction 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?>
<script>// Javascript program for// binomial coefficients // Function to print// binomial tablefunction printbinomial(max){    for (let m = 0; m <= max; m++)    {        document.write(m);        let binom = 1;        for (let 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;                         document.write(" " + binom + " ");        }    document.write("<br>");    }} // Driver Codelet max = 10;printbinomial(max); // This code is contributed by _saurabh_jaiswal</script>
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
 
jit_t
_saurabh_jaiswal
simmytarika5
binomial coefficient
Combinatorial
Mathematical
Mathematical
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Find the Number of Permutations that satisfy the given condition in an array
Ways to sum to N using Natural Numbers up to K with repetitions allowed
Probability of getting K heads in N coin tosses
Number of handshakes such that a person shakes hands only once
Count ways to partition a Binary String such that each substring contains exactly two 0s
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
C++ Data Types
Merge two sorted arrays
Coin Change | DP-7