Calculate nCr using Pascal’s Triangle
A useful application of Pascal’s triangle is the calculation of combinations. The formula to find nCr is n! / r! * (n – r)! which is also the formula for a cell of Pascal’s triangle.
Input: n = 5, r = 3
Output: 10
n! / r! * (n – r)! = 5! / 3! * (2!) = 120 / 12 = 10Input: n = 7, r = 2
Output: 21
Approach: The idea is to store the Pascal’s triangle in a matrix then the value of nCr will be the value of the cell at nth row and rth column.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Initialize the matrix with 0 int l[1001][1001] = { 0 }; int initialize() { // 0C0 = 1 l[0][0] = 1; for (int i = 1; i < 1001; i++) { // Set every nCr = 1 where r = 0 l[i][0] = 1; for (int j = 1; j < i + 1; j++) { // Value for the current cell of Pascal's triangle l[i][j] = (l[i - 1][j - 1] + l[i - 1][j]); } } } // Function to return the value of nCr int nCr(int n, int r) { // Return nCr return l[n][r]; } // Driver code int main() { // Build the Pascal's triangle initialize(); int n = 8; int r = 3; cout << nCr(n, r); } // This code is contributed by ihritik |
Java
// Java implementation of the approach class GFG { // Initialize the matrix with 0 static int l[][] = new int[1001][1001]; static void initialize() { // 0C0 = 1 l[0][0] = 1; for (int i = 1; i < 1001; i++) { // Set every nCr = 1 where r = 0 l[i][0] = 1; for (int j = 1; j < i + 1; j++) { // Value for the current cell of Pascal's triangle l[i][j] = (l[i - 1][j - 1] + l[i - 1][j]); } } } // Function to return the value of nCr static int nCr(int n, int r) { // Return nCr return l[n][r]; } // Driver code public static void main(String[] args) { // Build the Pascal's triangle initialize(); int n = 8; int r = 3; System.out.println(nCr(n, r)); } } // This code is contributed by ihritik |
Python3
# Python3 implementation of the approach # Initialize the matrix with 0 l = [[0 for i in range(1001)] for j in range(1001)] def initialize(): # 0C0 = 1 l[0][0] = 1 for i in range(1, 1001): # Set every nCr = 1 where r = 0 l[i][0] = 1 for j in range(1, i + 1): # Value for the current cell of Pascal's triangle l[i][j] = (l[i - 1][j - 1] + l[i - 1][j]) # Function to return the value of nCr def nCr(n, r): # Return nCr return l[n][r] # Driver code # Build the Pascal's triangle initialize() n = 8r = 3print(nCr(n, r)) |
C#
// C# implementation of the approach using System; class GFG { // Initialize the matrix with 0 static int[, ] l = new int[1001, 1001]; static void initialize() { // 0C0 = 1 l[0, 0] = 1; for (int i = 1; i < 1001; i++) { // Set every nCr = 1 where r = 0 l[i, 0] = 1; for (int j = 1; j < i + 1; j++) { // Value for the current cell of Pascal's triangle l[i, j] = (l[i - 1, j - 1] + l[i - 1, j]); } } } // Function to return the value of nCr static int nCr(int n, int r) { // Return nCr return l[n, r]; } // Driver code public static void Main() { // Build the Pascal's triangle initialize(); int n = 8; int r = 3; Console.WriteLine(nCr(n, r)); } } // This code is contributed by ihritik |
56
Recommended Posts:
- Program to calculate the Area and Perimeter of Incircle of an Equilateral Triangle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle
- Program to calculate value of nCr
- Calculate the Discriminant Value
- Calculate the Sum of GCD over all subarrays
- C program to calculate the value of nPr
- Program to calculate the value of sin(x) and cos(x) using Expansion
- Efficient program to calculate e^x
- Program to Calculate e^x by Recursion
- Program to calculate the value of nCr Efficiently
- Write a program to calculate pow(x,n)
- Calculate area of pentagon with given diagonal
- Program to calculate GST from original and net prices
- Program to calculate distance between two points in 3 D
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.

