Problem no 15: Calculate npr.

 QUESTION :

 

Write a program to calculate nPrnPr 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

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.