Question -
Given the value of n and r, you have to calculate the permutation and combination.
Example -
Input : n = 10 , r = 5
Output : ncr = 252 , npr = 30240
Approach -
We solve this question with the help of standard formula of permutation and combination.
i.e.
npr = n!/(n-r)!
ncp = n!/(r!*(n-r)!)
Implementation -
//permutation and combination
#include<iostream>
using namespace std;
int fact(int num)
{
int fact=1;
for(int i=1;i<=num;i++)
fact*=i;
return fact;
}
int main()
{
int n,r;
cout<<"Enter the value of n : ";cin>>n;
cout<<"Enter the value of r : ";cin>>r;
double ncr,npr;
//ncr=n!/(r!*(n-r)!)
//npr=n!/(n-r)!
npr=fact(n)/fact(n-r);
ncr=fact(n)/(fact(r)*fact(n-r));
cout<<"ncr = "<<ncr<<endl<<"npr = "<<npr<<endl;
}
Time complexity - O(n)
Comments
Post a Comment