Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
C program to calculate the value of nPr?
Permutations, nPr can also be represented as P(n,r), is a mathematical formula to find the number of ways to arrange r objects from n objects where order matters. The formula of P(n, r) is n! / (n − r)!.
The number of permutations on a set of n elements is given by n! where "!" represents factorial.
Syntax
nPr = n! / (n - r)!
Where:
- n is the total number of objects
- r is the number of objects to be selected
- ! denotes factorial
Example
Let's calculate P(5, 4) which means selecting and arranging 4 objects from 5 objects −
#include <stdio.h>
long int fact(int x) {
int i;
long int f = 1;
for(i = 2; i <= x; i++) {
f = f * i;
}
return f;
}
int main() {
int n, r;
long int npr;
n = 5;
r = 4;
npr = fact(n) / fact(n - r);
printf("P(%d, %d) = %ld<br>", n, r, npr);
return 0;
}
P(5, 4) = 120
Explanation
The calculation works as follows:
P(5, 4) = 5! / (5-4)!
= 5! / 1!
= 120 / 1
= 120
Where 5! = 1 × 2 × 3 × 4 × 5 = 120
Key Points
- nPr represents permutations where order matters
- The factorial function calculates n! = 1 × 2 × 3 × ... × n
- Use
long intfor larger factorial values to avoid overflow
Conclusion
The nPr formula efficiently calculates permutations by dividing n! by (n-r)!. This mathematical approach is useful in probability and combinatorics problems where arrangement order is significant.
Advertisements
