Problem no 15: Calculate npr.
QUESTION :
Write a program to calculate nPr. nPr represents n permutation r and value of nPr is (n!) / (n-r)!.
Example 1:
Input: n = 2, r = 1
Output: 2
Explaination: 2!/(2-1)! = 2!/1! = (2*1)/1 = 2.
Example 2:
Input: n = 3, r = 3
Output: 6
Explaination: 3!/(3-3)! = 3!/0! = 6/1 = 6.
ANSWER:
long long int factorial(int N){
long long int result=1;
for(int i=1;i<=N;i++)
{
result*=i;
}
return result;
}
long long nPr(int n, int r){
int diff=n-r;
long long int x=factorial(n); ..calling factorial
long long int y=factorial(diff); ..calling factorial
long long z=x/y;
return z;
}
Comments
Post a Comment