nPr (permutation)
:#include <stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
}
else {
return n * factorial(n-1);
}
}
int nPr(int n, int r) {
return factorial(n) / factorial(n-r);
}
int main() {
int n, r;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Enter the value of r: ");
scanf("%d", &r);
if (n < r) {
printf("Invalid input: n should be greater than or equal to r\n");
}
else {
printf("%dP%d = %d\n", n, r, nPr(n, r));
}
return 0;
}
Enter the value of n: 5
Enter the value of r: 7
Invalid input: n should be greater than or equal to r
Enter the value of n: 7
Enter the value of r: 3
7P3 = 210
nPr function
calculates the permutation of n
and r
using the formula n!/(n-r)!
. main() function
prompts the user to enter the values of n
and r
, checks whether n
is greater than or equal to r
, and prints the result of the permutation if the input is valid.