# C Program to calculate Permutation and Combination

Last updated on September 23, 2020

The following is a C program to compute Permutation and Combination:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | ```
/**************************************************
Program to calculate Permutation and Combination
*
* Enter n: 10
* Enter r: 4
* Permutation = 5040
* Combination = 210
***************************************************/
#include<stdio.h> // include stdio.h library
long permutation(int n, int r);
long combination(int n, int r);
long factorial(int num);
int main(void)
{
int n, r;
printf("Enter n: ");
scanf("%d", &n);
printf("Enter r: ");
scanf("%d", &r);
printf("Permutation = %ld\n", permutation(n, r));
printf("Combination = %ld", combination(n, r));
return 0; // return 0 to operating system
}
long permutation(int n, int r)
{
return factorial(n) / factorial(n-r);
}
long combination(int n, int r)
{
return permutation(n, r) / factorial(r);
}
long factorial(int num)
{
long long fact = 1;
while(num > 0)
{
fact *= num;
num--;
}
return fact;
}
``` |

**Expected Output:** 1st run:

1 2 3 4 | ```
Enter n: 5
Enter r: 3
Permutation = 60
Combination = 10
``` |

2nd run:

1 2 3 4 | ```
Enter n: 5
Enter r: 0
Permutation = 1
Combination = 1
``` |

## How it works #

Nothing magical here we are simply using the formula to calculate permutation and combination.

The permutations of `n`

different objects taken `r`

at a time is given by:

\begin{gather*}

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

\end{gather*}

The combinations of `n`

different objects taken `r`

at a time is given by:

\begin{gather*}

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

\end{gather*}

**Recommended Reading:**

- C Program to find the factorial of a number
- C Program to find the largest of three numbers
- C Program to calculate the power of a number
- C Program to print the two digit number in words
- C Program to print Twin prime numbers between two ranges

Load Comments