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 ![]()
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++
// 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; } |
Java
// 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 |
Python3
# 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 = 10printbinomial(max) # This code is contributed by "Sharad_bhardwaj". |
C#
// 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. |
PHP
<?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 ?> |
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
Recommended Posts:
- Sum of Binomial coefficients
- Mathematics | PnC and Binomial Coefficients
- Sum of squares of binomial coefficients
- Sum of product of consecutive Binomial Coefficients
- Find sum of even index binomial coefficients
- Program to print binomial expansion series
- Program to print multiplication table of a number
- Binomial Coefficient | DP-9
- Sum of product of r and rth Binomial Coefficient (r * nCr)
- Corollaries of Binomial Theorem
- Binomial Random Variables
- Maximum binomial coefficient term value
- Space and time efficient Binomial Coefficient
- Middle term in the binomial expansion series
- Eggs dropping puzzle (Binomial Coefficient and Binary Search Solution)
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



