English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The following is an example of calculating combinations using factorial.
#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, result; cout << "Enter n : "; cin >> n; cout << "\nEnter r : "; cin >> r; result = fact(n / (fact(r * fact(n-r)); cout << "\nThe result: " << result; return 0; }
Output result
Enter n: 10 Enter r: 4 The result: 210
In the above program, the code exists infact()
The function calculates the factorial of a number.
if (n == 0 || n == 1) return 1; else return n * fact(n - 1);
In thismain()
In the method, the combination of two numbers entered by the user is calculated. The variable "result" stores the calculated value of the combination using factorial.
cout << "Enter n : "; cin >> n; cout << "\nEnter r : "; cin >> r; result = fact(n / (fact(r * fact(n-r));