
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compute Combinations Using Factorials in C++
The following is an example to compute combinations using factorials.
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, 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
Enter n : 10 Enter r : 4 The result : 210
In the above program, the code is present in fact() function to calculate the factorial of numbers.
if (n == 0 || n == 1) return 1; else return n * fact(n - 1);
In the main() function, two numbers of combination are entered by user. Variable ‘result’ is storing the calculated value of combination using factorial.
cout<<"Enter n : "; cin>>n; cout<<"\nEnter r : "; cin>>r; result = fact(n) / (fact(r) * fact(n-r));
Advertisements