C Program to Find the Value of Permutation (nPr) and Combination (nCr)

nCr nPr

C programs to find the value of $nPr$ and $nCr$ have been shown here. $nPr$, $nCr$ are referred as the permutation and combination respectively. Here $n$ is the total no of objects, $r$ is the no of objects taken at a time for arrangement and $r \leq n$. The formula to find the value of $nPr$ and $nCr$ are given below.


 $nPr = \frac{n!}{(n-r)!}$


 $nCr = \frac{n!}{(n-r)! \cdot r!}$


Example:

Suppose $n = 5$ and $r = 3$


$5P3 = \frac{5!}{(5-3)!} = \frac{5!}{2!} = \frac{120}{2} = 60$


$5C3 = \frac{5!}{(5-3)!\cdot3!} = \frac{5!}{2!\cdot3!} = \frac{120}{12} = 10$







1. Program & output to find the value of permutation (nPr) and combination (nCr)




1.1. C Program & output to find the value of permutation (nPr) and combination (nCr) using iteration

Code has been copied
/****************************************
      alphabetacoder.com
C Program to find the value of
permutation (nPr) and combination (nCr) 
****************************************/

#include<stdio.h>

// function to calculate factorial
long factorial(int num) {
    // declare variables
    int i;
    long fact = 1;
    // calculate factorial
    for (i = 2; i <= num; i++)
        fact *= i;

    // return result
    return fact;
}

// function to calculate nCr
long calculate_ncr(int n, int r) {
    // return result
    return factorial(n) / (factorial(n - r) * factorial(r));
}

// function to calculate nPr
long calculate_npr(int n, int r) {
    // return result
    return factorial(n) / factorial(n - r);
}

int main() {
    // delclare variables
    int n, r;

    // take input
    printf("Enter the value of n: ");
    scanf("%d", & n);
    printf("Enter the value of r: ");
    scanf("%d", & r);

    // display results by calling function
    printf("%dP%d = %ld\n", n, r, calculate_npr(n, r));
    printf("%dC%d = %ld\n", n, r, calculate_ncr(n, r));

    return 0;
}

Output


Enter the value of n: 10

Enter the value of r: 7

10P7 = 604800

10C7 = 120





1.2. C Program & output to find the value of permutation (nPr) and combination (nCr) using recursion

Code has been copied
/*********************************************
	   alphabetacoder.com
C Program to find the value of permutation
(nPr) and combination (nCr) using recursion 
*********************************************/

#include<stdio.h>

// recursive function to calculate factorial
long factorial(int num) {
    if (num <= 1)
        return 1;

    return num * factorial(num - 1);
}

// function to calculate nCr
long calculate_ncr(int n, int r) {
    // return result
    return factorial(n) / (factorial(n - r) * factorial(r));
}

// recursive function to calculate nPr
long calculate_npr(int n, int r) {
    // return result
    return factorial(n) / factorial(n - r);
}

int main() {
    // delclare variables
    int n, r;

    // take input
    printf("Enter the value of n: ");
    scanf("%d", & n);
    printf("Enter the value of r: ");
    scanf("%d", & r);

    // display results by calling function
    printf("%dP%d = %ld\n", n, r, calculate_npr(n, r));
    printf("%dC%d = %ld\n", n, r, calculate_ncr(n, r));

    return 0;
}

Output


Enter the value of n: 6

Enter the value of r: 4

6P4 = 360

6C4 = 15