Program for nCr using Recursion
Last Updated :
20 Sep, 2025
Given two integers n and r, the task is to compute the value of nCr (i.e., the number of ways to choose r elements from a set of n elements) using Recursion.
Examples:
Input: n = 5, r = 2
Output: 10
Explanation: There are 10 ways to choose 2 elements from 5. Using recursion: 5C2 = 4C1 + 4C2 = 4 + 6 = 10.
Input: n = 3, r = 1
Output: 3
Explanation: Choosing 1 element from 3 can be done in 3 ways. Base case applies directly.
Input: n = 6, r = 3
Output: 20
[Approach - 1] Using Pascal's Rule - O(2^n) Time and O(n) Space
The idea is to use recursion to break down the original combination problem into smaller subproblems by repeatedly reducing either n or r, and summing up their results until the base cases are met.
Recursive Relation:
The recursive relation lies in this line: return nCr(n - 1, r - 1) + nCr(n - 1, r);
This is based on Pascal’s Rule: nCr = (n-1)C(r-1) + (n-1)Cr
Here:
- (n - 1, r - 1) considers choosing the current element.
- (n - 1, r) considers skipping the current element.
- The sum of both gives total ways to choose r elements from n.
At every recursive step, the function branches into two subproblems, reducing the problem size until it reaches a known base value.
Base Cases:
The base cases are:
- if (r == 0 || r == n) return 1;
- if (r == 1) return n;
These serve as the termination conditions:
- r == 0 or r == n: Only one way to choose none or all elements.
- r == 1: Exactly n ways to choose one element from n.
C++
// C++ code to compute nCr using Recursion
#include <iostream>
using namespace std;
// Recursive function to calculate nCr
int nCr(int n, int r) {
// Base Case 1: If r = 0 or r = n,
// then nCr = 1
if (r == 0 || r == n) {
return 1;
}
// Base Case 2: If r = 1, then nCr = n
if (r == 1) {
return n;
}
// Recursive relation:
// nCr = (n-1)C(r-1) + (n-1)Cr
return nCr(n - 1, r - 1) + nCr(n - 1, r);
}
// Driver code
int main() {
int n = 5, r = 2;
cout << nCr(n, r);
return 0;
}
Java
// Java code to compute nCr using Recursion
class GfG {
// Recursive function to calculate nCr
static int nCr(int n, int r) {
// Base Case 1: If r = 0 or r = n,
// then nCr = 1
if (r == 0 || r == n) {
return 1;
}
// Base Case 2: If r = 1, then nCr = n
if (r == 1) {
return n;
}
// Recursive relation:
// nCr = (n-1)C(r-1) + (n-1)Cr
return nCr(n - 1, r - 1) + nCr(n - 1, r);
}
public static void main(String[] args) {
int n = 5, r = 2;
System.out.println(nCr(n, r));
}
}
Python
# Python code to compute nCr using Recursion
# Recursive function to calculate nCr
def nCr(n, r):
# Base Case 1: If r = 0 or r = n,
# then nCr = 1
if r == 0 or r == n:
return 1
# Base Case 2: If r == 1, then nCr = n
if r == 1:
return n
# Recursive relation:
# nCr = (n-1)C(r-1) + (n-1)Cr
return nCr(n - 1, r - 1) + nCr(n - 1, r)
if __name__ == "__main__":
n = 5
r = 2
print(nCr(n, r))
C#
// C# code to compute nCr using Recursion
using System;
class GfG {
// Recursive function to calculate nCr
static int nCr(int n, int r) {
// Base Case 1: If r = 0 or r = n,
// then nCr = 1
if (r == 0 || r == n) {
return 1;
}
// Base Case 2: If r = 1, then nCr = n
if (r == 1) {
return n;
}
// Recursive relation:
// nCr = (n-1)C(r-1) + (n-1)Cr
return nCr(n - 1, r - 1) + nCr(n - 1, r);
}
static void Main(string[] args) {
int n = 5, r = 2;
Console.WriteLine(nCr(n, r));
}
}
JavaScript
// JavaScript code to compute nCr using Recursion
// Recursive function to calculate nCr
function nCr(n, r) {
// Base Case 1: If r = 0 or r = n,
// then nCr = 1
if (r === 0 || r === n) {
return 1;
}
// Base Case 2: If r = 1, then nCr = n
if (r === 1) {
return n;
}
// Recursive relation:
// nCr = (n-1)C(r-1) + (n-1)Cr
return nCr(n - 1, r - 1) + nCr(n - 1, r);
}
// Driver Code
let n = 5, r = 2;
console.log(nCr(n, r));
Time Complexity: O(2^n), Every call branches into two, creating a binary recursion tree.
Space Complexity: O(n), Maximum depth of recursive calls is n.
[Approach - 2] Using Mathematical Transformation - O(r) Time and O(r) Space
The idea is to compute nCr recursively using a mathematical transformation rather than expanding full factorials. The thought process is to simplify the standard formula for nCr using a relation with its previous value, which avoids redundant computation. The observation here is that each nCr can be derived from nCr-1 by multiplying with (n - r + 1) and dividing by r, leading to an efficient recursion. This reduces the recursive tree depth compared to the previous approach.
Derivation:
nCr = n! / (r! * (n-r)!)
Also, nCr-1 = n! / ((r-1)! * (n - (r-1))!)
Hence,
- nCr * r! * (n - r)! = nCr-1 * (r-1)! * (n - (r-1))!
- nCr * r * (n-r)! = nCr-1 * (n-r+1)! [eliminating (r - 1)! from both side]
- nCr * r = nCr-1 * (n-r+1)
Therefore,
nCr = nCr-1 * (n-r+1) / r
C++
// C++ code to compute nCr using recursion
#include <iostream>
using namespace std;
// Recursive function to calculate nCr
int nCr(int n, int r) {
// Base Case 1: If r = 0, then nCr = 1
if (r == 0) {
return 1;
}
// Base Case 2: If r = 1, then nCr = n
if (r == 1) {
return n;
}
// Recursive relation:
// nCr = nCr-1 * (n - r + 1) / r
return nCr(n, r - 1) * (n - r + 1) / r;
}
// Driver code
int main() {
int n = 5, r = 2;
cout << nCr(n, r);
return 0;
}
Java
// Java code to compute nCr using recursion
class GfG {
// Recursive function to calculate nCr
static int nCr(int n, int r) {
// Base Case 1: If r = 0, then nCr = 1
if (r == 0) {
return 1;
}
// Base Case 2: If r = 1, then nCr = n
if (r == 1) {
return n;
}
// Recursive relation:
// nCr = nCr-1 * (n - r + 1) / r
return nCr(n, r - 1) * (n - r + 1) / r;
}
public static void main(String[] args) {
int n = 5, r = 2;
System.out.println(nCr(n, r));
}
}
Python
# Python code to compute nCr using recursion
# Recursive function to calculate nCr
def nCr(n, r):
# Base Case 1: If r = 0, then nCr = 1
if r == 0:
return 1
# Base Case 2: If r == 1, then nCr = n
if r == 1:
return n
# Recursive relation:
# nCr = nCr-1 * (n - r + 1) / r
return nCr(n, r - 1) * (n - r + 1) // r
if __name__ == "__main__":
n = 5
r = 2
print(nCr(n, r))
C#
// C# code to compute nCr using recursion
using System;
class GfG {
// Recursive function to calculate nCr
static int nCr(int n, int r) {
// Base Case 1: If r = 0, then nCr = 1
if (r == 0) {
return 1;
}
// Base Case 2: If r = 1, then nCr = n
if (r == 1) {
return n;
}
// Recursive relation:
// nCr = nCr-1 * (n - r + 1) / r
return nCr(n, r - 1) * (n - r + 1) / r;
}
static void Main() {
int n = 5, r = 2;
Console.WriteLine(nCr(n, r));
}
}
JavaScript
// JavaScript code to compute nCr using recursion
// Recursive function to calculate nCr
function nCr(n, r) {
// Base Case 1: If r = 0, then nCr = 1
if (r === 0) {
return 1;
}
// Base Case 2: If r == 1, then nCr = n
if (r === 1) {
return n;
}
// Recursive relation:
// nCr = nCr-1 * (n - r + 1) / r
return nCr(n, r - 1) * (n - r + 1) / r;
}
// Driver Code
let n = 5, r = 2;
console.log(nCr(n, r));
Time Complexity: O(r), Each call reduces r by 1 until it hits 0 or 1.
Space Complexity: O(r), Due to recursive stack space proportional to r.
Explore
DSA Fundamentals
Data Structures
Algorithms
Advanced
Interview Preparation
Practice Problem