
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
How to calculate combination and permutation in C++?
Combination and permutation are a part of Combinatorics. Permutation is 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 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.
Number of permutations when there are total n elements and r elements need to be arranged.
Number of combinations when there are total n elements and r elements need to be selected.
A program that calculates combination and permutation in C++ is given as follows.
Example
#include <iostream> using namespace std; int fact(int n) { if (n == 0 || n == 1) return 1; else return n * fact(n - 1); } int main() { int n, r, comb, per; cout<<"Enter n : "; cin>>n; cout<<"\nEnter r : "; cin>>r; comb = fact(n) / (fact(r) * fact(n-r)); cout << "\nCombination : " << comb; per = fact(n) / fact(n-r); cout << "\nPermutation : " << per; return 0; }
Output
The output of the above program is as follows.
Enter n : 5 Enter r : 3 Combination : 10 Permutation : 60
- Related Articles
- Permutation and Combination in Java
- Permutation and Combination in Python?
- How can SciPy be used to calculate the permutations and combination values in Python?
- How to calculate the resistances of n number of resistors connected in parallel combination?
- How to create combination of multiple vectors in R?
- Next Permutation in Python
- Permutation Sequence in C++
- Find Permutation in C++
- Permutation in String in C++
- How to find the combination of matrix values in R?
- Minimum removal to make palindrome permutation in C++
- Program to find decode XORed permutation in Python
- Lexicographically next permutation in C++
- Letter Case Permutation in C++
- Palindrome Permutation II in C++

Advertisements