Calculate Combination and Permutation in C++



Combination and permutation are a part of combinatorics. The permutation is known as the different arrangements that a set of elements can make if the elements are taken one at a time, some at a time, or all at a time. Combination is the different ways of selecting elements if the elements are taken one at a time, some at a time, or all at a time.

In this article, we have the value of 'n' and 'r' such that 'r' should be less than n. Our task is the find the permutation and combination using the value of 'n' and 'r' in C++.

Formula of Permutation and Combination

The formula for calculating the permutation and combination is as follows:

  • Permutation: The number of permutations when there are total 'n' elements and 'r' elements need to be arranged is given as:
  • Permutations

  • Combination: The number of combinations when there are total 'n' elements and 'r' elements need to be selected is given as:
  • Combinations

Here is a list of approaches to calculate combination and permutation in C++ with step-wise explanations and complete example codes:

Using Recursion

In this approach, we have used a recursive function to calculate the factorial of the given number.

  • We have used the fact() function that recursively calls itself until it reaches its terminating condition, i.e. when the value of n is 0 or 1. In each call, it multiplies n with n-1.
  • In the main() function, we check the condition that r should be greater than 0 and less than n.
  • Then, we use the fact() function to calculate the permutation and combination and store them in their respective variables.

Example

The following example implements the above steps to calculate the permutation and combination in C++ using recursion.

#include <iostream>
using namespace std;

// Recursive factorial function
long long fact(int n) {
    if (n == 0 || n == 1)
        return 1;
    return n * fact(n - 1);
}

int main() {
    // Hardcoded values for n and r
    int n = 10, r = 3; // Example values, change as needed

    // Input validation
    if (r > n || n < 0 || r < 0) {
        cout << "Invalid input: ensure 0 <= r <= n" << endl;
        return 1;
    }

    long long comb = fact(n) / (fact(r) * fact(n - r));
    long long per = fact(n) / fact(n - r);

    cout << "Value for n: " << n << " and r: " << r << " is: ";
    cout << "\nCombination : " << comb << endl;
    cout << "Permutation : " << per << endl;

    return 0;
}

The output of the above code is:

Value for n: 10 and r: 3 is: 
Combination : 120
Permutation : 720

Using Iteration

The following approach uses iteration instead of recursion to calculate the factorial of the number. We follow the same steps as the previous approach except for the recursion part.

Example

Here is an example of implementing the above steps to calculate the permutation and combination in C++ using iteration.

#include <iostream>
using namespace std;

// Iterative factorial function
long long factorial(int n) {
    long long result = 1;
    for (int i = 2; i <= n; ++i)
        result *= i;
    return result;
}

int main() {
    // Hardcoded values for n and r
    int n = 10, r = 3; // Change these values as needed

    // Input validation
    if (n < 0 || r < 0 || r > n) {
        cout << "Invalid input: Ensure 0 <= r <= n" << endl;
        return 1;
    }

    long long comb = factorial(n) / (factorial(r) * factorial(n - r));
    long long per = factorial(n) / factorial(n - r);
    
    cout << "Value for n: " << n << " and r: " << r << " is: ";
    cout << "\nCombination : " << comb << endl;
    cout << "Permutation : " << per << endl;

    return 0;
}

The output of the above code is:

Value for n: 10 and r: 3 is: 
Combination : 120
Permutation : 720

Complexity Comparison

Here is a comparison of the time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Using Recursion O(n) O(n)
Using Iteration O(n) O(1)
Updated on: 2025-05-02T15:19:10+05:30

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements