# C program to calculate the value of nCr

Here, we are going to learn how to calculate the value of nCr using C program?
Submitted by Nidhi, on August 11, 2021

Problem Solution:

Read the value of n and r and calculate the nCr.

nCr:

nCr known as combination, is the method of selection of 'r' objects from a set of 'n' objects where the order of selection does not matter.

To find the value of nCr, we use the formula: nCr = n!/[r!( n-r)!]

Program:

The source code to calculate the value of nCr is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.

```// C program to calculate the value of nCr

#include <stdio.h>

int getFactorial(int num)
{
int f = 1;
int i = 0;

if (num == 0)
return 1;

for (i = 1; i <= num; i++)
f = f * i;

return f;
}

int main()
{
int n = 0;
int r = 0;

int nCr = 0;

printf("Enter the value of N: ");
scanf("%d", &n);

printf("Enter the value of R: ");
scanf("%d", &r);

nCr = getFactorial(n) / (getFactorial(r) * getFactorial(n - r));

printf("The nCr is: %d\n", nCr);

return 0;
}
```

Output:

```RUN 1:
Enter the value of N: 7
Enter the value of R: 3
The nCr is: 35

RUN 2:
Enter the value of N: 5
Enter the value of R: 0
The nCr is: 1

RUN 3:
Enter the value of N: 0
Enter the value of R: 5
The nCr is: 0

RUN 4:
Enter the value of N: 11
Enter the value of R: 5
The nCr is: 462
```

Explanation:

In the above program, we created two functions getFactorial(), main(). The getFactorial() function is used to find the factorial of the given number and return the result to the calling function.

In the main() function, we read the values of n and r. Then we calculate the nCr with the help of the getFactorial() function and print the result on the console screen.