Skip to content
Related Articles

Related Articles

Program to calculate value of nCr

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 28 Sep, 2022
Improve Article
Save Article

Following are the common definitions of Binomial Coefficients

  • A binomial coefficient C(n, k) can be defined as the coefficient of Xk 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, The task is to find the value of NCr

Examples : 

Input: N = 5, r = 2
Output: 10 
Explanation: The value of 5C2 is 10

Input: N = 3, r = 1
Output: 3

Approach: Below is the idea to solve the problem:

The total number of ways for selecting r elements out of n options are nCr = (n!) / (r! * (n-r)!) 
where n! = 1 * 2 * . . . * n.

Below is the Implementation of the above approach:

C++




// 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 n
int fact(int n)
{
      if(n==0)
      return 1;
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
 
// Driver code
int main()
{
    int n = 5, r = 3;
    cout << nCr(n, r);
    return 0;
}

C




#include <stdio.h>
 
int factorial(int n) {
      if(n == 0)
      return 1;
    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

Java




// Java program To calculate
// The Value Of nCr
class GFG {
 
static int nCr(int n, int r)
{
    return fact(n) / (fact(r) *
                  fact(n - r));
}
 
// Returns factorial of n
static int fact(int n)
{
      if(n==0)
      return 1;
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}
 
// Driver code
public 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




# Python 3 program To calculate
# The Value Of nCr
 
def nCr(n, r):
 
    return (fact(n) / (fact(r)
                * fact(n - r)))
 
# Returns factorial of n
def fact(n):
    if n == 0:
        return 1
    res = 1
     
    for i in range(2, n+1):
        res = res * i
         
    return res
 
# Driver code
n = 5
r = 3
print(int(nCr(n, r)))
 
# This code is contributed
# by Smitha

C#




// C# program To calculate
// The Value Of nCr
using System;
 
class GFG {
 
static int nCr(int n, int r)
{
   return fact(n) / (fact(r) *
                 fact(n - r));
}
 
// Returns factorial of n
static int fact(int n)
{
      if(n==0)
      return 1;
    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
// PHP program To calculate
// the Value Of nCr
 
 
function nCr( $n, $r)
{
    return fact($n) / (fact($r) *
                  fact($n - $r));
}
 
// Returns factorial of n
function fact( $n)
{
      if($n == 0)
      return 1;
    $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.
?>

Javascript




<script>
 
// Javascript program To calculate The Value Of nCr
 
function nCr(n, r)
{
    return fact(n) / (fact(r) * fact(n - r));
}
 
// Returns factorial of n
function fact(n)
{
      if(n==0)
      return 1;
    var res = 1;
    for (var i = 2; i <= n; i++)
        res = res * i;
    return res;
}
 
// Driver code
var n = 5, r = 3;
document.write(nCr(n, r));
 
 
</script>

Output

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!