Open In App

Program to calculate value of nCr

Last Updated : 13 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given two numbers n and r, The task is to find the value of nCr . Combinations represent the number of ways to choose r elements from a set of n distinct elements, without regard to the order in which they are selected. The formula for calculating combinations is :

Combinations
Combinations Formula

Note: If r is greater than n, return 0.
It is guaranteed that the value of nCr will fit within a 32-bit integer.

Examples

Input: n = 5, r = 2
Output: 10 
Explanation: The value of 5C2 is calculated as 5! / ((5−2)! * 2!​)= 10.

Input: n = 2, r = 4
Output: 0
Explanation: Since r is greater than n, thus 2C4 = 0

Input: n = 5, r = 0
Output: 1
Explanation: The value of 5C0 is calculated as 5!/(5−0)!*0! = 5!/5!*0! = 1.

[Naive Approach] Using Recursion - O(2^n) Time and O(n) Space

The idea is to use a recursive function to calculate the value of nCr. The base cases are:

  • if r is greater than n, return 0 (there are no combinations possible)
  • if r is 0 or r is n, return 1 (there is only 1 combination possible in these cases)

For other values of n and r, the function calculates the value of nCr by adding the number of combinations possible by including the current element and the number of combinations possible by not including the current element. 

C++
#include <iostream>
using namespace std;

int nCr(int n, int r){
    
    if (r > n)
        return 0;
    if (r == 0 || r == n)
        return 1;
    return nCr(n - 1, r - 1) + nCr(n - 1, r);
}

int main(){
    
    int n = 5;
    int r = 2;
    cout << nCr(5, 2); 
    return 0;
}
C
#include <stdio.h>

int nCr(int n, int r){
    
    if (r > n)
        return 0;
    if (r == 0 || r == n)
        return 1;
    return nCr(n - 1, r - 1) + nCr(n - 1, r);
}

int main(){
    int n = 5;
    int r = 2;
    printf("%d\n", nCr(n, r));
    return 0;
}
Java
import java.util.*;

class GfG {
    
    public static int nCr(int n, int r){
        
        if (r > n)
            return 0;
        if (r == 0 || r == n)
            return 1;
        return nCr(n - 1, r - 1) + nCr(n - 1, r);
    }

    public static void main(String[] args){
        
        int  n = 5;
        int  r = 2;
        System.out.println(nCr(n, r)); 
    }
}
Python
def nCr(n, r):
    if r > n:
        return 0
    if r == 0 or r == n:
        return 1
    return nCr(n-1, r-1) + nCr(n-1, r)


if __name__ == "__main__":
    n = 5
    r = 2
    print(nCr(n, r))  
C#
using System;

class GfG {
    static public int nCr(int n, int r){
        
        if (r > n)
            return 0;
        if (r == 0 || r == n)
            return 1;
        return nCr(n - 1, r - 1) + nCr(n - 1, r);
    }

    static public void Main(string[] args){
        int n = 5;
        int r = 2;
        Console.WriteLine(nCr(n, r)); 
    }
}
JavaScript
function nCr(n, r) {
    if (r > n)
        return 0;
    if (r === 0 || r === n)
        return 1;
    return nCr(n-1, r-1) + nCr(n-1, r);
}

// Drive Code
let n = 5;
let r = 2;
console.log(nCr(5, 2)); 

Output
10

[Better Approach - 1] Using Factorial - O(n) Time and O(1) Space

This approach calculates the nCr using the factorial formula. It first computes the factorial of a given number by multiplying all integers from 1 to that number.

To find nCr, it calculates the factorial of n, r, and (n - r) separately, then applies the formula n! / (r!(n-r)!) to determine the result. Since factorial values grow rapidly, this method is inefficient for large values due to integer overflow and excessive computations.

Note: This approach may produce incorrect results due to integer overflow when handling large values of n and r.

C++
// CPP program To calculate The Value Of nCr
#include <bits/stdc++.h>
using namespace std;

// Returns factorial of n
int fact(int n){
    
    int res = 1;
    for (int i = 2; i <= n; i++)
        res = res * i;
    return res;
}

int nCr(int n, int r){
    if(r>n)
        return 0;
    return fact(n) / (fact(r) * fact(n - r));
}

// Driver code
int main()
{
    int n = 5, r = 2;
    cout << nCr(n, r);
    return 0;
}
C
#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) {
    if(r>n)
        return 0;
        
    return factorial(n) / (factorial(r) * factorial(n - r));
}

int main() {
    int n = 5, r = 2;
    
    printf("%d", nCr(n, r));
    return 0;
}

// This code was contributed by Omkar Prabhune
Java
// Java program To calculate
// The Value Of nCr
import java.io.*;

public class GfG {

    static int nCr(int n, int r){
        
        if(r>n)
            return 0;
            
        return fact(n) / (fact(r) * fact(n - r));
    }

    // Returns factorial of n
    static 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(String[] args){
        
        int n = 5, r = 2;
        System.out.println(nCr(n, r));
    }
}

// This code is Contributed by
// Smitha Dinesh Semwal.
Python
def nCr(n, r):
    if(r>n):
        return 0
        
    return (fact(n) // (fact(r) 
                * fact(n - r)))

# Returns factorial of n
def fact(n):
    res = 1
    
    for i in range(2, n+1):
        res = res * i
        
    return res

# Driver code
if __name__ == "__main__":
    n = 5
    r = 2
    print(int(nCr(n, r)))
C#
// C# program To calculate
// The Value Of nCr
using System;

class GFG {

    static int nCr(int n, int r){
        
        if(r>n)
            return 0;
            
        return fact(n) / (fact(r) * fact(n - r));
    }

    // Returns factorial of n
    static 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 = 2;
        Console.Write(nCr(n, r));
    }
}
JavaScript
function nCr(n, r) {
    
    if(r>n)
        return 0;
    return fact(n) / (fact(r) * fact(n - r)); 
} 

// Returns factorial of n 
function fact(n) {
    
    var res = 1; 
    for (var i = 2; i <= n; i++) 
        res = res * i; 
    return res; 
} 

// Driver code 
var n = 5, r = 2; 
console.log (nCr(n, r)); 

Output
10

[Better Approach - 2] Avoiding Factorial Computations - O(n) Time and O(1) Space

  • The formula for nCr is n! / (r!(n-r)!).
  • Instead of computing full factorials, we avoid redundant calculations by recognizing that r! and (n-r)! share common terms that cancel out.
  • To optimize, we compute the product of numbers from r+1 to n and divide it by the product of numbers from 1 to (n-r).
  • Here, r is chosen as the maximum of r and (n-r) to reduce the number of multiplications.
  • This approach avoids large factorial values, reducing computational overhead and preventing integer overflow.

Note: This approach may produce incorrect results due to integer overflow when handling large values of n and r.

C++
#include <iostream>
using namespace std;

// calculate multiplication of natural numbers from start to end
double Multiplier(int start, int end){
    
    if (start == end)
        return start;
    double res = 1;
    while (start <= end){
        
        res *= start;
        start++;
    }
    return res;
}

int nCr(int n, int r){
    
    if (n < r)
        return 0;
    if (n == r || r == 0)
        return 1;

    int max_val = max(r, n - r);
    int min_val = min(r, n - r);
    double numerator = Multiplier(max_val + 1, n);
    double denominator = Multiplier(1, min_val);
    return int(numerator / denominator);
}

int main()
{
    int n = 5;
    int r = 2;
    std::cout << nCr(n, r) << std::endl;
    return 0;
}
C
#include <stdio.h>

// calculate multiplication of natural numbers from start to end
double Multiplier(int start, int end){
    
    if (start == end)
        return start;
    double res = 1;
    while (start <= end){
        
        res *= start;
        start++;
    }
    return res;
}

int nCr(int n, int r){
    
    if (n < r)
        return 0;
    if (n == r || r == 0)
        return 1;
    int max_val = (r > (n - r)) ? r : (n - r);
    int min_val = (r < (n - r)) ? r : (n - r);
    double numerator = Multiplier(max_val + 1, n);
    double denominator = Multiplier(1, min_val);
    return (int)(numerator / denominator);
}

int main(){
    
    int n = 5;
    int r = 2;
    printf("%d\n", nCr(n, r));
    return 0;
}
Java
// calculate multiplication of natural numbers from start to end
class GfG {
    
    public static double Multiplier(int start, int end) {
        
        if (start == end)
            return start;
        double res = 1;
        
        while (start <= end) {
            res *= start;
            start++;
        }
        return res;
    }

    public static int nCr(int n, int r) {
        
        if (n < r)
            return 0;
        if (n == r || r == 0)
            return 1;

        int max_val = Math.max(r, n - r);
        int min_val = Math.min(r, n - r);
        double numerator = Multiplier(max_val + 1, n);
        double denominator = Multiplier(1, min_val);
        return (int)(numerator / denominator);
    }

    public static void main(String[] args) {
        int n = 5;
        int r = 2;
        System.out.println(nCr(n, r));
    }
}
Python
# calculate multiplication of natural numbers from start to end
def Multiplier(start, end):
    if start == end:
        return start
    res = 1
    while start <= end:
        res *= start
        start += 1
    return res

def nCr(n, r):
    if n < r:
        return 0
    if n == r or r == 0:
        return 1

    max_val = max(r, n - r)
    min_val = min(r, n - r)
    numerator = Multiplier(max_val + 1, n)
    denominator = Multiplier(1, min_val)
    return numerator // denominator

if __name__ == "__main__":
    n = 5
    r = 2
    print(nCr(n, r))
C#
// calculate multiplication of natural numbers from start to end
using System;

class Program {
    static double Multiplier(int start, int end) {
        if (start == end)
            return start;
        double res = 1;
        while (start <= end) {
            res *= start;
            start++;
        }
        return res;
    }

    static int nCr(int n, int r) {
        if (n < r)
            return 0;
        if (n == r || r == 0)
            return 1;

        int max_val = Math.Max(r, n - r);
        int min_val = Math.Min(r, n - r);
        double numerator = Multiplier(max_val + 1, n);
        double denominator = Multiplier(1, min_val);
        return (int)(numerator / denominator);
    }

    static void Main() {
        int n = 5;
        int r = 2;
        Console.WriteLine(nCr(n, r));
    }
}
JavaScript
// calculate multiplication of natural numbers from start to end
function Multiplier(start, end) {
    if (start === end) {
        return start;
    }
    let res = 1;
    while (start <= end) {
        res *= start;
        start++;
    }
    return res;
}

function nCr(n, r) {
    if (n < r) {
        return 0;
    }
    if (n === r || r === 0) {
        return 1;
    }

    const max_val = Math.max(r, n - r);
    const min_val = Math.min(r, n - r);
    const numerator = Multiplier(max_val + 1, n);
    const denominator = Multiplier(1, min_val);
    return Math.round(numerator / denominator);
}

// Driver Code
const n = 5;
const r = 2;
console.log(nCr(n, r));

Output
10

[Expected Approach] By using Binomial Coefficient formula - O(r) Time and O(1) Space

  • 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.

Iterative way of calculating nCr   using binomial coefficient formula.

C++
#include <iostream>
using namespace std;
int nCr(int n, int r){
    
    double sum = 1;

    // Calculate the value of n choose
    // r using the binomial coefficient formula
    for (int i = 1; i <= r; i++){
        
        sum = sum * (n - r + i) / i;
    }
    return (int)sum;
}
int main(){
    
    int n = 5;
    int r = 2;
    cout << nCr(n, r);

    return 0;
}
C
#include <stdio.h>

int nCr(int n, int r){
    
    double sum = 1;
    
    // Calculate the value of n choose r 
    // using the binomial coefficient formula
    for(int i = 1; i <= r; i++){
        sum = sum * (n - r + i) / i;
    }
    return (int)sum;
}
int main() {
    int n = 5;
    int r = 2;
    
    printf("%d\n", nCr(n,r));
    return 0;
}
Java
import java.util.*;

class GfG{
    
    public static int nCr(int n, int r){
        
        double sum = 1;

        // Calculate the value of n choose r using the
        // binomial coefficient formula
        for (int i = 1; i <= r; i++) {
            sum = sum * (n - r + i) / i;
        }

        return (int)sum;
    }
    public static void main(String[] args){

        int n = 5;
        int r = 2;
        System.out.println(nCr(n,r));
    }
}
Python
def nCr(n, r):
    
    sum = 1

    # Calculate the value of n choose r 
    # using the binomial coefficient formula
    for i in range(1, r+1):
        sum = sum * (n - r + i) // i
    
    return sum
    
if __name__ == "__main__":
    n = 5
    r = 2
    
    print(nCr(n, r))
C#
using System;

// C# code implementation 
class GfG {
    static int nCr(int n, int r){
        double sum = 1;
        
        // Calculate the value of n choose r
        // using the binomial coefficient formula
        for(int i = 1; i <= r; i++){
            sum = sum * (n - r + i) / i;
        }
        return (int)sum;
    }
    static void Main() {
        int n = 5;
        int r = 2;
        
        Console.WriteLine(nCr(n,r));
    }
}
JavaScript
function nCr(n, r){
    let sum = 1;

    // Calculate the value of n choose r 
    // using the binomial coefficient formula
    for(let i = 1; i <= r; i++){
      sum = sum * (n - r + i) / i;
    }
    
    return Math.floor(sum);
}

// Driver Code
let n = 5;
let r = 2;

console.log(nCr(n,r));

Output
10

[Alternate Approach] Using Logarithmic Formula - O(r) Time and O(1)

Logarithmic formula for nCr is an alternative to the factorial formula that avoids computing factorials directly and it's more efficient for large values of n and r. It uses the identity log(n!) = log(1) + log(2) + ... + log(n) to express the numerator and denominator of the nCr in terms of sums of logarithms which allows to calculate the nCr using the Logarithmic operations. This approach is faster and very efficient.

The logarithmic formula for nCr is: nCr = exp( log(n!) - log(r!) - log((n-r)!) )

C++
#include <bits/stdc++.h>
using namespace std;

// Calculates the binomial coefficient nCr using the logarithmic formula
int nCr(int n, int r) {
    
    // If r is greater than n, return 0
    if (r > n) return 0;
    
    // If r is 0 or equal to n, return 1
    if (r == 0 || n == r) return 1;
    
    // Initialize the logarithmic sum to 0
    double res = 0;
    
    // Calculate the logarithmic sum of 
    // the numerator and denominator using loop
    for (int i = 0; i < r; i++) {
        
        // Add the logarithm of (n-i) 
        // and subtract the logarithm of (i+1)
        res += log(n-i) - log(i+1);
    }
    
    // Convert logarithmic sum back to a normal number
    return (int)round(exp(res));
}

int main() {
    
    // Calculate nCr for n = 5 and r = 2
    int n = 5;
    int r = 2;
    cout << nCr(n, r) << endl;
    return 0;
}
C
#include <stdio.h>
#include <math.h>

// Calculates the binomial coefficient nCr using the logarithmic formula
int nCr(int n, int r) {
    
    // If r is greater than n, return 0
    if (r > n) return 0;
    
    // If r is 0 or equal to n, return 1
    if (r == 0 || n == r) return 1;
    
    // Initialize the logarithmic sum to 0
    double res = 0;
    
    // Calculate the logarithmic sum of the numerator and denominator using loop
    for (int i = 0; i < r; i++) {
        
        // Add the logarithm of (n-i) and subtract the logarithm of (i+1)
        res += log(n-i) - log(i+1);
    }
    
    // Convert logarithmic sum back to a normal number
    return (int)round(exp(res));
}

int main() {
    
    // Calculate nCr for n = 5 and r = 2
    int n = 5;
    int r = 2;
    printf("%d\n", nCr(n, r));
    return 0;
}
Java
import java.lang.Math;

// Calculates the binomial coefficient nCr using the logarithmic formula
public class GfG {
    public static int nCr(int n, int r) {
        
        // If r is greater than n, return 0
        if (r > n) return 0;
        
        // If r is 0 or equal to n, return 1
        if (r == 0 || n == r) return 1;
        
        // Initialize the logarithmic sum to 0
        double res = 0;
        
        // Calculate the logarithmic sum of the numerator and denominator using loop
        for (int i = 0; i < r; i++) {
            
            // Add the logarithm of (n-i) and subtract the logarithm of (i+1)
            res += Math.log(n-i) - Math.log(i+1);
        }
        
        // Convert logarithmic sum back to a normal number
        return (int)Math.round(Math.exp(res));
    }

    public static void main(String[] args) {
        
        // Calculate nCr for n = 5 and r = 2
        int n = 5;
        int r = 2;
        System.out.println(nCr(n, r));
    }
}
Python
import math

# Calculates the binomial coefficient nCr using the logarithmic formula
def nCr(n, r):
    
    # If r is greater than n, return 0
    if r > n:
        return 0
    
    # If r is 0 or equal to n, return 1
    if r == 0 or n == r:
        return 1
    
    # Initialize the logarithmic sum to 0
    res = 0
    
    # Calculate the logarithmic sum of the numerator and denominator using loop
    for i in range(r):
        
        # Add the logarithm of (n-i) and subtract the logarithm of (i+1)
        res += math.log(n-i) - math.log(i+1)
    
    # Convert logarithmic sum back to a normal number
    return round(math.exp(res))

if __name__ == "__main__":
    n = 5
    r = 2
    print(nCr(n, r))
C#
using System;

class GfG{
    
    public static int nCr(int n, int r){
        
        // If r is greater than n, return 0
        if (r > n) return 0;

        // If r is 0 or equal to n, return 1
        if (r == 0 || n == r) return 1;

        // Initialize the logarithmic sum to 0
        double res = 0;

        // Calculate the logarithmic sum
        for (int i = 0; i < r; i++){
            
            res += Math.Log(n - i) - Math.Log(i + 1);
        }

        // Convert logarithmic result back to integer using exponentiation and rounding
        return (int)Math.Round(Math.Exp(res));
    }

    static void Main(string[] args){
        
        int n = 5;
        int r = 2;
        Console.WriteLine(nCr(n, r));
    }
}
JavaScript
// Calculates the binomial coefficient nCr using the logarithmic formula
function nCr(n, r) {
    
    // If r is greater than n, return 0
    if (r > n) return 0;
    
    // If r is 0 or equal to n, return 1
    if (r === 0 || n === r) return 1;
    
    // Initialize the logarithmic sum to 0
    let res = 0;
    
    // Calculate the logarithmic sum of the numerator and denominator using loop
    for (let i = 0; i < r; i++) {
        
        // Add the logarithm of (n-i) and subtract the logarithm of (i+1)
        res += Math.log(n-i) - Math.log(i+1);
    }
    
    // Convert logarithmic sum back to a normal number
    return Math.round(Math.exp(res));
}

// Calculate nCr for n = 5 and r = 2
const n = 5;
const r = 2;
console.log(nCr(n, r));

Output
10

Next Article

Similar Reads