Open In App

C program to calculate the value of nPr

Improve
Improve
Like Article
Like
Save
Share
Report

nPr represents n permutation r and value of nPr is (n!) / (n-r)!.
 

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 n: ");
    scanf("%d", &n);
 
    printf("Enter r: ");
    scanf("%d", &r);
 
    printf("%dP%d is %d", n, r, nPr(n, r));
 
    return 0;
}


Enter n: 5
Enter r: 2
5P2 is 20 

Time Complexity: O(n)

Auxiliary Space: O(n)

Please refer Permutation Coefficient for efficient methods to compute nPr.

 


Last Updated : 02 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads