# C program to calculate the value of nPr for a given value of n & r

In this C Programming example, we will implement the program to find the value of nPr for a given value of n & r and print the output on the console.

## 1. What is nPr in Probability – Mathematics?

`nPr` is a probability function that represents permutations. `nPr` represents the probability of selecting an ordered set of ‘r’ objects from a group of ‘n’ number of objects. The order of objects matters in the case of permutation. The formula to calculate nPr is –

```nPr(n,r) = n! / (n-r)!
Here,
n = the total number of items or net size.
r = It is the subnet size or total number of items chosen from sample.

n! is factorial(n) which is represented as 1*2*....*(n-1)*n
Similary the explanation for (n-r)! is also same.```
```Example:

Input
n: 4
r: 3

Formula
4!/(4-3)! = 24/1 = 24

Output
4P3 is 24```

Helpful topics to understand this program better are-

## 2. C Program to find the value of nPr for a given value of n & r

Let’s discuss the execution(kind of pseudocode) for the program to find the value of nPr for a given value of n & r in C.

1. Initially, the program will prompt the user to enter the values of n and r and then pass those values to the `nPr(n, r)` function.
2. In the `int nPr(int n, int r)` function, the factorial of the values are calculated using the `int fact(int n)` function.
3. After calculating the factorial the output is returned and printed on the console.

In this C Programming example, we have discussed how to find the value of nPr for a given value of n & r in C.

```#include <stdio.h>

int fact(int n) {
if (n <= 1) {
return 1;
}
return n * fact(n - 1);
}

int nPr(int n, int r) { return fact(n) / fact(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);

printf("The value of %dP%d is %d", n, r, nPr(n, r));
return 0;
}```

Note: Only whole positive integer numbers are valid.

```Output
Enter the value of n: 5
Enter the value of  r: 2
The value of 5P2 is 20```

## 3. Conclusion

In this C Programming example, we have discussed how to find the value of nPr for a given value of n & r and discussed the steps of the program in detail.

Please follow C Programming tutorials or the menu in the sidebar for the complete tutorial series.

Also for the example C programs please refer to C Programming Examples.

All examples are hosted on Github.

#### Recommended Books

An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding

Do not forget to share and Subscribe.

Happy coding!! 😊

### Recommended -

Subscribe
Notify of 