Program to calculate value of nCr

18 Jan, 2022
Following are common definition 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 an n-element set.
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 an n-element set.
Given two numbers n and r, find value of nCrExamples :  
Input :  n = 5, r = 2
Output : 10
The value of 5C2 is 10

Input : n = 3, r = 1
Output : 3
 
The idea is simply based on below formula. 
nCr = (n!) / (r! * (n-r)!)
 
C
C++
Java
Python 3
C#
PHP
Javascript
#include <stdio.h> int factorial(int n) {    int factorial = 1;    for (int i = 2; i <= n; i++)        factorial = factorial * i;    return factorial;} int nCr(int n, int r) {    return factorial(n) / (factorial(r) * factorial(n - r));} int main() {    int n = 5, r = 3;      printf("%d", nCr(n, r));    return 0;} // This code was contributed by Omkar Prabhune
// CPP program To calculate The Value Of nCr#include <bits/stdc++.h>using namespace std; int fact(int n); int nCr(int n, int r){    return fact(n) / (fact(r) * fact(n - r));} // Returns factorial of nint fact(int n){    int res = 1;    for (int i = 2; i <= n; i++)        res = res * i;    return res;} // Driver codeint main(){    int n = 5, r = 3;    cout << nCr(n, r);    return 0;}
// Java program To calculate// The Value Of nCrclass GFG { static int nCr(int n, int r){    return fact(n) / (fact(r) *                  fact(n - r));} // Returns factorial of nstatic int fact(int n){    int res = 1;    for (int i = 2; i <= n; i++)        res = res * i;    return res;} // Driver codepublic static void main(String[] args){    int n = 5, r = 3;    System.out.println(nCr(n, r));}} // This code is Contributed by// Smitha Dinesh Semwal.
# Python 3 program To calculate# The Value Of nCr def nCr(n, r):     return (fact(n) / (fact(r)                * fact(n - r))) # Returns factorial of ndef fact(n):     res = 1         for i in range(2, n+1):        res = res * i             return res # Driver coden = 5r = 3print(int(nCr(n, r))) # This code is contributed# by Smitha
// C# program To calculate// The Value Of nCrusing System; class GFG { static int nCr(int n, int r){   return fact(n) / (fact(r) *                 fact(n - r));} // Returns factorial of nstatic int fact(int n){    int res = 1;    for (int i = 2; i <= n; i++)        res = res * i;    return res;}    // Driver code   public static void Main()   {      int n = 5, r = 3;      Console.Write(nCr(n, r));   }} // This code is Contributed by nitin mittal.
<?php// PHP program To calculate// the Value Of nCr  function nCr( $n, $r){    return fact($n) / (fact($r) *                  fact($n - $r));} // Returns factorial of nfunction fact( $n){    $res = 1;    for ( $i = 2; $i <= $n; $i++)        $res = $res * $i;    return $res;}     // Driver code    $n = 5;    $r = 3;    echo nCr($n, $r);     // This code is contributed by vt_m.?>
<script> // Javascript program To calculate The Value Of nCr function nCr(n, r){    return fact(n) / (fact(r) * fact(n - r));} // Returns factorial of nfunction fact(n){    var res = 1;    for (var i = 2; i <= n; i++)        res = res * i;    return res;} // Driver codevar n = 5, r = 3;document.write(nCr(n, r));  </script>
10
 
Time Complexity: O(n)
Auxiliary Space: O(1)
More Efficient Solutions: Dynamic Programming | Set 9 (Binomial Coefficient) Space and time efficient Binomial Coefficient All Articles on Binomial Coefficient 
Smitha Dinesh Semwal
nitin mittal
vt_m
naimishrastogi
rutvik_56
opprabhune317
prophet1999
binomial coefficient
factorial
Combinatorial
Mathematical
School Programming
Mathematical
Combinatorial
factorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Count of subsets with sum equal to X
Count Derangements (Permutation such that no element appears in its original position)
Stack Permutations (Check if an array is stack permutation of other)
Find the K-th Permutation Sequence of first N natural numbers
Probability of getting K heads in N coin tosses
Set in C++ Standard Template Library (STL)
C++ Data Types
Merge two sorted arrays
Operators in C / C++
Algorithm to solve Rubik's Cube