Google News
logo
C program to calculate and print the value of nPr (Permutation)
In the following example of C program to calculate and print the value of nPr (permutation) :
Program :
#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;
}
Output :
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
* In this program, the factorial function is defined to calculate the factorial of a given integer. The nPr function calculates the permutation of n and r using the formula n!/(n-r)!.

* The 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.