# Binomial Coefficient | DP-9

The following are the common definitions of Binomial Coefficients

A binomial coefficient C(n, k) can be defined as the coefficient of x^k in the expansion of (1 + x)^n.

A binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects more formally, the number of k-element subsets (or k-combinations) of a n-element set.

The Problem
Write a function that takes two parameters n and k and returns the value of Binomial Coefficient C(n, k). For example, your function should return 6 for n = 4 and k = 2, and it should return 10 for n = 5 and k = 2.

1) Optimal Substructure
The value of C(n, k) can be recursively calculated using the following standard formula for Binomial Coefficients.

```   C(n, k) = C(n-1, k-1) + C(n-1, k)
C(n, 0) = C(n, n) = 1```

Following is a simple recursive implementation that simply follows the recursive structure mentioned above.

 `// A naive recursive C++ implementation` `#include ` `using` `namespace` `std;`   `// Returns value of Binomial Coefficient C(n, k)` `int` `binomialCoeff(``int` `n, ``int` `k)` `{` `    ``// Base Cases` `    ``if` `(k > n)` `        ``return` `0;` `    ``if` `(k == 0 || k == n)` `        ``return` `1;`   `    ``// Recur` `    ``return` `binomialCoeff(n - 1, k - 1)` `           ``+ binomialCoeff(n - 1, k);` `}`   `/* Driver code*/` `int` `main()` `{` `    ``int` `n = 5, k = 2;` `    ``cout << ``"Value of C("` `<< n << ``", "` `<< k << ``") is "` `         ``<< binomialCoeff(n, k);` `    ``return` `0;` `}`   `// This is code is contributed by rathbhupendra`

 `// A Naive Recursive Implementation` `#include `   `// Returns value of Binomial Coefficient C(n, k)` `int` `binomialCoeff(``int` `n, ``int` `k)` `{` `    ``// Base Cases` `    ``if` `(k > n)` `        ``return` `0;` `    ``if` `(k == 0 || k == n)` `        ``return` `1;`   `    ``// Recur` `    ``return` `binomialCoeff(n - 1, k - 1)` `           ``+ binomialCoeff(n - 1, k);` `}`   `/* Driver program to test above function*/` `int` `main()` `{` `    ``int` `n = 5, k = 2;` `    ``printf``(``"Value of C(%d, %d) is %d "``, n, k,` `           ``binomialCoeff(n, k));` `    ``return` `0;` `}`

 `// JAVA Code for Dynamic Programming |` `// Set 9 (Binomial Coefficient)` `import` `java.util.*;`   `class` `GFG {`   `    ``// Returns value of Binomial` `    ``// Coefficient C(n, k)` `    ``static` `int` `binomialCoeff(``int` `n, ``int` `k)` `    ``{`   `        ``// Base Cases` `        ``if` `(k > n)` `            ``return` `0``;` `        ``if` `(k == ``0` `|| k == n)` `            ``return` `1``;`   `        ``// Recur` `        ``return` `binomialCoeff(n - ``1``, k - ``1``)` `            ``+ binomialCoeff(n - ``1``, k);` `    ``}`   `    ``/* Driver program to test above function */` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `n = ``5``, k = ``2``;` `        ``System.out.printf(``"Value of C(%d, %d) is %d "``, n, k,` `                          ``binomialCoeff(n, k));` `    ``}` `}`   `// This code is contributed by Arnav Kr. Mandal.`

 `# A naive recursive Python implementation`     `def` `binomialCoeff(n, k):`   `    ``if` `k > n:` `        ``return` `0` `    ``if` `k ``=``=` `0` `or` `k ``=``=` `n:` `        ``return` `1`   `    ``# Recursive Call` `    ``return` `binomialCoeff(n``-``1``, k``-``1``) ``+` `binomialCoeff(n``-``1``, k)`     `# Driver Program to test ht above function` `n ``=` `5` `k ``=` `2` `print` `"Value of C(%d,%d) is (%d)"` `%` `(n, k,` `                                     ``binomialCoeff(n, k))`   `# This code is contributed by Nikhil Kumar Singh (nickzuck_007)`

 `// C# Code for Dynamic Programming |` `// Set 9 (Binomial Coefficient)` `using` `System;`   `class` `GFG {`   `    ``// Returns value of Binomial` `    ``// Coefficient C(n, k)` `    ``static` `int` `binomialCoeff(``int` `n, ``int` `k)` `    ``{`   `        ``// Base Cases` `        ``if` `(k > n)` `            ``return` `0;` `        ``if` `(k == 0 || k == n)` `            ``return` `1;`   `        ``// Recur` `        ``return` `binomialCoeff(n - 1, k - 1)` `            ``+ binomialCoeff(n - 1, k);` `    ``}`   `    ``/* Driver program to test above function */` `    ``public` `static` `void` `Main()` `    ``{` `        ``int` `n = 5, k = 2;` `        ``Console.Write(``"Value of C("` `+ n + ``","` `+ k + ``") is "` `                      ``+ binomialCoeff(n, k));` `    ``}` `}`   `// This code is contributed by Sam007.`

 ` ``\$n``)` `        ``return` `0;` `    ``if` `(``\$k``==0 || ``\$k``==``\$n``)` `        ``return` `1;` `    `  `    ``// Recur` `    ``return` `binomialCoeff(``\$n` `- 1, ``\$k` `- 1) + ` `               ``binomialCoeff(``\$n` `- 1, ``\$k``);` `}`   `    ``// Driver Code` `    ``\$n` `= 5; ` `    ``\$k` `= 2;` `    ``echo` `"Value of C"``,``"("``,``\$n` `,``\$k``,``") is "` `               ``, binomialCoeff(``\$n``, ``\$k``);`   `// This code is contributed by aj_36` `?>`

Output
`Value of C(5, 2) is 10`

2) Overlapping Subproblems
It should be noted that the above function computes the same subproblems again and again. See the following recursion tree for n = 5 an k = 2. The function C(3, 1) is called two times. For large values of n, there will be many common subproblems.

Binomial Coefficients Recursion tree for C(5,2)

Since the same subproblems are called again, this problem has Overlapping Subproblems property. So the Binomial Coefficient problem has both properties (see this and this) of a dynamic programming problem. Like other typical Dynamic Programming(DP) problems, re-computations of the same subproblems can be avoided by constructing a temporary 2D-array C[][] in a bottom-up manner. Following is Dynamic Programming based implementation.

 `// A Dynamic Programming based solution that uses` `// table C[][] to calculate the Binomial Coefficient` `#include ` `using` `namespace` `std;`   `// Prototype of a utility function that` `// returns minimum of two integers` `int` `min(``int` `a, ``int` `b);`   `// Returns value of Binomial Coefficient C(n, k)` `int` `binomialCoeff(``int` `n, ``int` `k)` `{` `    ``int` `C[n + 1][k + 1];` `    ``int` `i, j;`   `    ``// Caculate value of Binomial Coefficient` `    ``// in bottom up manner` `    ``for` `(i = 0; i <= n; i++) {` `        ``for` `(j = 0; j <= min(i, k); j++) {` `            ``// Base Cases` `            ``if` `(j == 0 || j == i)` `                ``C[i][j] = 1;`   `            ``// Calculate value using previously` `            ``// stored values` `            ``else` `                ``C[i][j] = C[i - 1][j - 1] + C[i - 1][j];` `        ``}` `    ``}`   `    ``return` `C[n][k];` `}`   `// A utility function to return` `// minimum of two integers` `int` `min(``int` `a, ``int` `b) { ``return` `(a < b) ? a : b; }`   `// Driver Code` `int` `main()` `{` `    ``int` `n = 5, k = 2;` `    ``cout << ``"Value of C["` `<< n << ``"]["` `<< k << ``"] is "` `         ``<< binomialCoeff(n, k);` `}`   `// This code is contributed by Shivi_Aggarwal`

 `// A Dynamic Programming based solution` `// that uses table C[][] to` `// calculate the Binomial Coefficient` `#include `   `// Prototype of a utility function that` `// returns minimum of two integers` `int` `min(``int` `a, ``int` `b);`   `// Returns value of Binomial Coefficient C(n, k)` `int` `binomialCoeff(``int` `n, ``int` `k)` `{` `    ``int` `C[n + 1][k + 1];` `    ``int` `i, j;`   `    ``// Caculate value of Binomial Coefficient` `    ``// in bottom up manner` `    ``for` `(i = 0; i <= n; i++) {` `        ``for` `(j = 0; j <= min(i, k); j++) {` `            ``// Base Cases` `            ``if` `(j == 0 || j == i)` `                ``C[i][j] = 1;`   `            ``// Calculate value using` `            ``// previously stored values` `            ``else` `                ``C[i][j] = C[i - 1][j - 1] + C[i - 1][j];` `        ``}` `    ``}`   `    ``return` `C[n][k];` `}`   `// A utility function to return` `// minimum of two integers` `int` `min(``int` `a, ``int` `b) { ``return` `(a < b) ? a : b; }`   `/* Drier program to test above function*/` `int` `main()` `{` `    ``int` `n = 5, k = 2;` `    ``printf``(``"Value of C(%d, %d) is %d "``, n, k,` `           ``binomialCoeff(n, k));` `    ``return` `0;` `}`

 `// A Dynamic Programming based` `// solution that uses table C[][] to` `// calculate the Binomial Coefficient`   `class` `BinomialCoefficient {` `    ``// Returns value of Binomial` `    ``// Coefficient C(n, k)` `    ``static` `int` `binomialCoeff(``int` `n, ``int` `k)` `    ``{` `        ``int` `C[][] = ``new` `int``[n + ``1``][k + ``1``];` `        ``int` `i, j;`   `        ``// Calculate  value of Binomial` `        ``// Coefficient in bottom up manner` `        ``for` `(i = ``0``; i <= n; i++) {` `            ``for` `(j = ``0``; j <= min(i, k); j++) {` `                ``// Base Cases` `                ``if` `(j == ``0` `|| j == i)` `                    ``C[i][j] = ``1``;`   `                ``// Calculate value using` `                ``// previously stored values` `                ``else` `                    ``C[i][j] = C[i - ``1``][j - ``1``] + C[i - ``1``][j];` `            ``}` `        ``}`   `        ``return` `C[n][k];` `    ``}`   `    ``// A utility function to return` `    ``// minimum of two integers` `    ``static` `int` `min(``int` `a, ``int` `b) { ``return` `(a < b) ? a : b; }`   `    ``/* Driver program to test above function*/` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``int` `n = ``5``, k = ``2``;` `        ``System.out.println(``"Value of C("` `+ n + ``","` `+ k` `                           ``+ ``") is "` `+ binomialCoeff(n, k));` `    ``}` `}` `/*This code is contributed by Rajat Mishra*/`

 `# A Dynamic Programming based Python` `# Program that uses table C[][]` `# to calculate the Binomial Coefficient`   `# Returns value of Binomial Coefficient C(n, k)`     `def` `binomialCoef(n, k):` `    ``C ``=` `[[``0` `for` `x ``in` `range``(k``+``1``)] ``for` `x ``in` `range``(n``+``1``)]`   `    ``# Calculate value of Binomial` `    ``# Coefficient in bottom up manner` `    ``for` `i ``in` `range``(n``+``1``):` `        ``for` `j ``in` `range``(``min``(i, k)``+``1``):` `            ``# Base Cases` `            ``if` `j ``=``=` `0` `or` `j ``=``=` `i:` `                ``C[i][j] ``=` `1`   `            ``# Calculate value using` `            ``# previously stored values` `            ``else``:` `                ``C[i][j] ``=` `C[i``-``1``][j``-``1``] ``+` `C[i``-``1``][j]`   `    ``return` `C[n][k]`     `# Driver program to test above function` `n ``=` `5` `k ``=` `2` `print``(``"Value of C["` `+` `str``(n) ``+` `"]["` `+` `str``(k) ``+` `"] is "` `      ``+` `str``(binomialCoef(n, k)))`   `# This code is contributed by Bhavya Jain`

 `// A Dynamic Programming based solution that` `// uses table C[][] to calculate the Binomial` `// Coefficient` `using` `System;`   `class` `GFG {`   `    ``// Returns value of Binomial Coefficient` `    ``// C(n, k)` `    ``static` `int` `binomialCoeff(``int` `n, ``int` `k)` `    ``{` `        ``int``[, ] C = ``new` `int``[n + 1, k + 1];` `        ``int` `i, j;`   `        ``// Calculate value of Binomial` `        ``// Coefficient in bottom up manner` `        ``for` `(i = 0; i <= n; i++) {` `            ``for` `(j = 0; j <= Math.Min(i, k); j++) {` `                ``// Base Cases` `                ``if` `(j == 0 || j == i)` `                    ``C[i, j] = 1;`   `                ``// Calculate value using previously` `                ``// stored values` `                ``else` `                    ``C[i, j] = C[i - 1, j - 1] + C[i - 1, j];` `            ``}` `        ``}`   `        ``return` `C[n, k];` `    ``}`   `    ``// A utility function to return minimum` `    ``// of two integers` `    ``static` `int` `min(``int` `a, ``int` `b) { ``return` `(a < b) ? a : b; }`   `    ``/* Driver program to test above function*/` `    ``public` `static` `void` `Main()` `    ``{` `        ``int` `n = 5, k = 2;` `        ``Console.WriteLine(``"Value of C("` `+ n + ``","` `+ k` `                          ``+ ``") is "` `+ binomialCoeff(n, k));` `    ``}` `}`   `// This code is contributed by anuj_67.`

 ``

Output
`Value of C is 10`

Time Complexity: O(n*k)
Auxiliary Space: O(n*k)

Following is a space-optimized version of the above code. The following code only uses O(k). Thanks to AK for suggesting this method.

 `// C++ program for space optimized Dynamic Programming` `// Solution of Binomial Coefficient` `#include ` `using` `namespace` `std;`   `int` `binomialCoeff(``int` `n, ``int` `k)` `{` `    ``int` `C[k + 1];` `    ``memset``(C, 0, ``sizeof``(C));`   `    ``C = 1; ``// nC0 is 1`   `    ``for` `(``int` `i = 1; i <= n; i++) {` `        ``// Compute next row of pascal triangle using` `        ``// the previous row` `        ``for` `(``int` `j = min(i, k); j > 0; j--)` `            ``C[j] = C[j] + C[j - 1];` `    ``}` `    ``return` `C[k];` `}`   `/* Driver code*/` `int` `main()` `{` `    ``int` `n = 5, k = 2;` `    ``printf``(``"Value of C(%d, %d) is %d "``, n, k,` `           ``binomialCoeff(n, k));` `    ``return` `0;` `}`

 `// JAVA Code for Dynamic Programming |` `// Set 9 (Binomial Coefficient)` `import` `java.util.*;`   `class` `GFG {`   `    ``static` `int` `binomialCoeff(``int` `n, ``int` `k)` `    ``{` `        ``int` `C[] = ``new` `int``[k + ``1``];`   `        ``// nC0 is 1` `        ``C[``0``] = ``1``;`   `        ``for` `(``int` `i = ``1``; i <= n; i++) {` `            ``// Compute next row of pascal` `            ``// triangle using the previous row` `            ``for` `(``int` `j = Math.min(i, k); j > ``0``; j--)` `                ``C[j] = C[j] + C[j - ``1``];` `        ``}` `        ``return` `C[k];` `    ``}`   `    ``/* Driver code  */` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `n = ``5``, k = ``2``;` `        ``System.out.printf(``"Value of C(%d, %d) is %d "``, n, k,` `                          ``binomialCoeff(n, k));` `    ``}` `}`

 `# Python program for Optimized` `# Dynamic Programming solution to` `# Binomail Coefficient. This one` `# uses the concept of pascal` `# Triangle and less memory`     `def` `binomialCoeff(n, k):`   `    ``# Declaring an empty array` `    ``C ``=` `[``0` `for` `i ``in` `xrange``(k``+``1``)]` `    ``C[``0``] ``=` `1`  `# since nC0 is 1`   `    ``for` `i ``in` `range``(``1``, n``+``1``):`   `        ``# Compute next row of pascal triangle using` `        ``# the previous row` `        ``j ``=` `min``(i, k)` `        ``while` `(j > ``0``):` `            ``C[j] ``=` `C[j] ``+` `C[j``-``1``]` `            ``j ``-``=` `1`   `    ``return` `C[k]`     `# Driver Code` `n ``=` `5` `k ``=` `2` `print` `"Value of C(%d,%d) is %d"` `%` `(n, k, binomialCoeff(n, k))`   `# This code is contribtued by Nikhil Kumar Singh(nickzuck_007)`

 `// C# Code for Dynamic Programming |` `// Set 9 (Binomial Coefficient)` `using` `System;`   `class` `GFG {`   `    ``static` `int` `binomialCoeff(``int` `n, ``int` `k)` `    ``{` `        ``int``[] C = ``new` `int``[k + 1];`   `        ``// nC0 is 1` `        ``C = 1;`   `        ``for` `(``int` `i = 1; i <= n; i++) {` `            ``// Compute next row of pascal` `            ``// triangle using the previous` `            ``// row` `            ``for` `(``int` `j = Math.Min(i, k); j > 0; j--)` `                ``C[j] = C[j] + C[j - 1];` `        ``}` `        ``return` `C[k];` `    ``}`   `    ``/* Driver Code */` `    ``public` `static` `void` `Main()` `    ``{` `        ``int` `n = 5, k = 2;` `        ``Console.WriteLine(``"Value of C("` `+ n + ``" "` `+ k` `                          ``+ ``") is "` `+ binomialCoeff(n, k));` `    ``}` `}`   `// This code is contribtued by anuj_67.`

 ` 0; ``\$j``--)` `            ``\$C``[``\$j``] = ``\$C``[``\$j``] + ``\$C``[``\$j` `- 1];` `    ``}` `    ``return` `\$C``[``\$k``];` `}`   `// Driver Code` `\$n` `= 5; ``\$k` `= 2;` `echo` `"Value of C[\$n, \$k] is "``. ` `        ``binomialCoeff(``\$n``, ``\$k``);` `    `  `// This code is contributed by mits.` `?>`

Output
`Value of C(5, 2) is 10`

Time Complexity: O(n*k)
Auxiliary Space: O(k)
Explanation:
1==========>> n = 0, C(0,0) = 1
1–1========>> n = 1, C(1,0) = 1, C(1,1) = 1
1–2–1======>> n = 2, C(2,0) = 1, C(2,1) = 2, C(2,2) = 1
1–3–3–1====>> n = 3, C(3,0) = 1, C(3,1) = 3, C(3,2) = 3, C(3,3)=1
1–4–6–4–1==>> n = 4, C(4,0) = 1, C(4,1) = 4, C(4,2) = 6, C(4,3)=4, C(4,4)=1
So here every loop on i, builds i’th row of pascal triangle, using (i-1)th row
At any time, every element of array C will have some value (ZERO or more) and in next iteration, value for those elements comes from previous iteration.
In statement,
C[j] = C[j] + C[j-1]
The right-hand side represents the value coming from the previous iteration (A row of Pascal’s triangle depends on the previous row). The left-Hand side represents the value of the current iteration which will be obtained by this statement.

```Let's say we want to calculate C(4, 3),
i.e. n=4, k=3:

All elements of array C of size 4 (k+1) are
initialized to ZERO.

i.e. C = C = C = C = C = 0;
Then C is set to 1

For i = 1:
C = C + C = 0 + 1 = 1 ==>> C(1,1) = 1

For i = 2:
C = C + C = 0 + 1 = 1 ==>> C(2,2) = 1
C = C + C = 1 + 1 = 2 ==>> C(2,1) = 2

For i=3:
C = C + C = 0 + 1 = 1 ==>> C(3,3) = 1
C = C + C = 1 + 2 = 3 ==>> C(3,2) = 3
C = C + C = 2 + 1 = 3 ==>> C(3,1) = 3

For i=4:
C = C + C = 0 + 1 = 1 ==>> C(4,4) = 1
C = C + C = 1 + 3 = 4 ==>> C(4,3) = 4
C = C + C = 3 + 3 = 6 ==>> C(4,2) = 6
C = C + C = 3 + 1 = 4 ==>> C(4,1) = 4

C(4,3) = 4 is would be the answer in our example.```

Memoization Approach: The idea is to create a lookup table and follow the recursive top-down approach. Before computing any value, we check if it is already in the lookup table. If yes, we return the value. Else we compute the value and store it in the lookup table. Following is the Top-down approach of dynamic programming to finding the value of the Binomial Coefficient.

 `// A Dynamic Programming based ` `// solution that uses` `// table dp[][] to calculate ` `// the Binomial Coefficient` `// A naive recursive approach ` `// with table C++ implementation` `#include ` `using` `namespace` `std;`   `// Returns value of Binomial Coefficient C(n, k)` `int` `binomialCoeffUtil(``int` `n, ``int` `k, ``int``** dp)` `{` `    ``// If value in lookup table then return` `    ``if` `(dp[n][k] != -1) ``//     ` `        ``return` `dp[n][k];`   `    ``// store value in a table before return` `    ``if` `(k == 0) {` `        ``dp[n][k] = 1;` `        ``return` `dp[n][k];` `    ``}` `    `  `    ``// store value in table before return` `    ``if` `(k == n) {` `        ``dp[n][k] = 1; ` `        ``return` `dp[n][k];` `    ``}` `    `  `    ``// save value in lookup table before return` `    ``dp[n][k] = binomialCoeffUtil(n - 1, k - 1, dp) +` `               ``binomialCoeffUtil(n - 1, k, dp);` `    ``return` `dp[n][k];` `}`   `int` `binomialCoeff(``int` `n, ``int` `k)` `{` `    ``int``** dp; ``// make a temporary lookup table` `    ``dp = ``new` `int``*[n + 1];`   `    ``// loop to create table dynamically` `    ``for` `(``int` `i = 0; i < (n + 1); i++) {` `        ``dp[i] = ``new` `int``[k + 1];` `    ``}`   `    ``// nested loop to initialise the table with -1` `    ``for` `(``int` `i = 0; i < (n + 1); i++) {` `        ``for` `(``int` `j = 0; j < (k + 1); j++) {` `            ``dp[i][j] = -1;` `        ``}` `    ``}`   `    ``return` `binomialCoeffUtil(n, k, dp);` `}`   `/* Driver code*/` `int` `main()` `{` `    ``int` `n = 5, k = 2;` `    ``cout << ``"Value of C("` `<< n << ``", "` `<< k << ``") is "` `         ``<< binomialCoeff(n, k) << endl;` `    ``return` `0;` `}`   `// This is code is contributed by MOHAMMAD MUDASSIR`

 `// A Dynamic Programming based ` `// solution that uses` `// table dp[][] to calculate ` `// the Binomial Coefficient` `// A naive recursive approach ` `// with table Java implementation` `import` `java.util.*;` `class` `GFG{`   `// Returns value of Binomial ` `// Coefficient C(n, k)` `static` `int` `binomialCoeffUtil(``int` `n, ``int` `k, ` `                             ``Vector []dp)` `{` `  ``// If value in lookup table ` `  ``// then return` `  ``if` `(dp[n].get(k) != -``1``)     ` `    ``return` `dp[n].get(k);`   `  ``// store value in a table ` `  ``// before return` `  ``if` `(k == ``0``) ` `  ``{` `    ``dp[n].add(k, ``1``);` `    ``return` `dp[n].get(k);` `  ``}`   `  ``// store value in table ` `  ``// before return` `  ``if` `(k == n) ` `  ``{` `    ``dp[n].add(k, ``1``); ` `    ``return` `dp[n].get(k);` `  ``}`   `  ``// save value in lookup table` `  ``// before return` `  ``dp[n].add(k, binomialCoeffUtil(n - ``1``, ` `                                 ``k - ``1``, dp) +` `               ``binomialCoeffUtil(n - ``1``, ` `                                 ``k, dp));` `  ``return` `dp[n].get(k);` `}`   `static` `int` `binomialCoeff(``int` `n, ``int` `k)` `{` `  ``// Make a temporary lookup table` `  ``Vector []dp = ``new` `Vector[n+``1``];`   `  ``// Loop to create table dynamically` `  ``for` `(``int` `i = ``0``; i < (n + ``1``); i++) ` `  ``{` `    ``dp[i] = ``new` `Vector();` `    ``for``(``int` `j = ``0``; j <= k; j++)` `      ``dp[i].add(-``1``);` `  ``}` `  ``return` `binomialCoeffUtil(n, k, dp);` `}`   `// Driver code` `public` `static` `void` `main(String[] args)` `{` `  ``int` `n = ``5``, k = ``2``;` `  ``System.out.print(``"Value of C("` `+ n + ` `                   ``", "` `+ k + ``") is "` `+ ` `                   ``binomialCoeff(n, k) + ``"\n"``);` `}` `}`   `// This code is contributed by Rajput-Ji`

 `# A Dynamic Programming based solution ` `# that uses table dp[][] to calculate ` `# the Binomial Coefficient. A naive ` `# recursive approach with table ` `# Python3 implementation `   `# Returns value of Binomial ` `# Coefficient C(n, k) ` `def` `binomialCoeffUtil(n, k, dp):` `    `  `    ``# If value in lookup table then return ` `    ``if` `dp[n][k] !``=` `-``1``: ` `        ``return` `dp[n][k] `   `    ``# Store value in a table before return ` `    ``if` `k ``=``=` `0``:` `        ``dp[n][k] ``=` `1` `        ``return` `dp[n][k] ` `    `  `    ``# Store value in table before return ` `    ``if` `k ``=``=` `n: ` `        ``dp[n][k] ``=` `1` `        ``return` `dp[n][k] ` `    `  `    ``# Save value in lookup table before return ` `    ``dp[n][k] ``=` `(binomialCoeffUtil(n ``-` `1``, k ``-` `1``, dp) ``+` `                ``binomialCoeffUtil(n ``-` `1``, k, dp))` `                `  `    ``return` `dp[n][k] `   `def` `binomialCoeff(n, k):` `    `  `    ``# Make a temporary lookup table ` `    ``dp ``=` `[ [ ``-``1` `for` `y ``in` `range``(k ``+` `1``) ] ` `                ``for` `x ``in` `range``(n ``+` `1``) ] `   `    ``return` `binomialCoeffUtil(n, k, dp)`   `# Driver code` `n ``=` `5` `k ``=` `2`   `print``(``"Value of C("` `+` `str``(n) ``+` `               ``", "` `+` `str``(k) ``+` `") is"``,` `               ``binomialCoeff(n, k)) `   `# This is code is contributed by Prateek Gupta `

 `// C# program for the ` `// above approach`   `// A Dynamic Programming based ` `// solution that uses` `// table [,]dp to calculate ` `// the Binomial Coefficient` `// A naive recursive approach ` `// with table C# implementation` `using` `System;` `using` `System.Collections.Generic;` `class` `GFG{`   `// Returns value of Binomial ` `// Coefficient C(n, k)` `static` `int` `binomialCoeffUtil(``int` `n, ``int` `k, ` `                             ``List<``int``> []dp)` `{` `  ``// If value in lookup table ` `  ``// then return` `  ``if` `(dp[n][k] != -1)     ` `    ``return` `dp[n][k];`   `  ``// store value in a table ` `  ``// before return` `  ``if` `(k == 0) ` `  ``{` `    ``dp[n][k] = 1;` `    ``return` `dp[n][k];` `  ``}`   `  ``// store value in table ` `  ``// before return` `  ``if` `(k == n) ` `  ``{` `    ``dp[n][k] = 1;` `    ``return` `dp[n][k];` `  ``}`   `  ``// save value in lookup table` `  ``// before return` `  ``dp[n][k] = binomialCoeffUtil(n - 1, ` `                               ``k - 1, dp) +` `             ``binomialCoeffUtil(n - 1, ` `                               ``k, dp);` `  ``return` `dp[n][k];` `}`   `static` `int` `binomialCoeff(``int` `n, ``int` `k)` `{` `  ``// Make a temporary lookup table` `  ``List<``int``> []dp = ``new` `List<``int``>[n + 1];`   `  ``// Loop to create table dynamically` `  ``for` `(``int` `i = 0; i < (n + 1); i++) ` `  ``{` `    ``dp[i] = ``new` `List<``int``>();`   `    ``for``(``int` `j = 0; j <= k; j++)` `      ``dp[i].Add(-1);` `  ``}` `  ``return` `binomialCoeffUtil(n, k, dp);` `}`   `// Driver code` `public` `static` `void` `Main(String[] args)` `{` `  ``int` `n = 5, k = 2;` `  ``Console.Write(``"Value of C("` `+ n + ` `                ``", "` `+ k + ``") is "` `+ ` `                ``binomialCoeff(n, k) + ``"\n"``);` `}` `}`   `// This code is contributed by 29AjayKumar`

Output
`Value of C(5, 2) is 10`

See this for Space and time efficient Binomial Coefficient

References:
http://www.csl.mtu.edu/cs4321/www/Lectures/Lecture%2015%20-%20Dynamic%20Programming%20Binomial%20Coefficients.htm