C Programming Code Examples C > Mathematics Code Examples program to calculate and print the value of nPr program to calculate and print the value of nPr In the following program we are calculating the value of nPr based on the given values of n and r. nPr can also be represented as P(n,r). The formula of P(n, r) is: n! / (n - r)!. For example P(5, 2) = 5! / (5-2)! => 120 / 6 = 20. #include <stdio.h> void main() { int n, r, npr_var; printf("Enter the value of n:"); scanf("%d", &n); printf("\nEnter the value of r:"); scanf("%d", &r); /* nPr is also known as P(n,r), the formula is: * P(n,r) = n! / (n - r)! For 0 <= r <= n. */ npr_var = fact(n) / fact(n - r); printf("\nThe value of P(%d,%d) is: %d",n,r,npr_var); } // Function for calculating factorial int fact(int num) { int k = 1, i; // factorial of 0 is 1 if (num == 0) { return(k); } else { for (i = 1; i <= num; i++) { k = k * i; } } return(k); }