Factorial and Permutation

Write a function to find the permutations of k items taken from set of n items. It should make use of the factorial function. The formula for permutation is given as

$$^nP_k = \frac{n!}{(n-k)!}$$

#include<iostream>
using namespace std;
int fact(int a){
	if(a<=1) return 1;
	else return a*fact(a-1);
}
int perm(int n,int k){
	if(n<0 || k<0) return 0;
	else if(n<k) return 0;
	else return fact(n)/fact(n-k);
}
int main(){
	int n,k,p;
	cout<<"Enter n : ";
	cin>>n;
	cout<<"Enter k : ";
	cin>>k;
	p = perm(n,k);
	cout<<"The permutations are "<<p;
}
output of the above program. permutations
output of the above program

Write the program to find the combinations. The formula for combination is given as

$$^nC_k = \frac{n!}{(n-k)!(k)!}$$

One Reply to “Factorial and Permutation”

Leave a Reply

Your email address will not be published. Required fields are marked *