# Program to calculate value of nCr

Following are common definition of Binomial Coefficients.

1. A binomial coefficient C(n, k) can be defined as the coefficient of X^k in the expansion of (1 + X)^n.
2. A binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects; more formally, the number of k-element subsets (or k-combinations) of an n-element set.

Given two numbers n and r, find value of nCr

Examples :

```Input :  n = 5, r = 2
Output : 30
The value of 5C2 is 10

Input : n = 3, r = 1
Output : 3
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

The idea is simply based on below formula.

nCr = (n!) / (r! * (n-r)!)

## C++

 `// CPP program To calculate The Value Of nCr ` `#include ` `using` `namespace` `std; ` ` `  `int` `fact(``int` `n); ` ` `  `int` `nCr(``int` `n, ``int` `r) ` `{ ` `    ``return` `fact(n) / (fact(r) * fact(n - r)); ` `} ` ` `  `// Returns factorial of n ` `int` `fact(``int` `n) ` `{ ` `    ``int` `res = 1; ` `    ``for` `(``int` `i = 2; i <= n; i++) ` `        ``res = res * i; ` `    ``return` `res; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 5, r = 3; ` `    ``cout << nCr(n, r); ` `    ``return` `0; ` `} `

## Java

 `// Java program To calculate  ` `// The Value Of nCr ` `class` `GFG { ` ` `  `static` `int` `nCr(``int` `n, ``int` `r) ` `{ ` `    ``return` `fact(n) / (fact(r) * ` `                  ``fact(n - r)); ` `} ` ` `  `// Returns factorial of n ` `static` `int` `fact(``int` `n) ` `{ ` `    ``int` `res = ``1``; ` `    ``for` `(``int` `i = ``2``; i <= n; i++) ` `        ``res = res * i; ` `    ``return` `res; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `n = ``5``, r = ``3``; ` `    ``System.out.println(nCr(n, r)); ` `} ` `} ` ` `  `// This code is Contributed by ` `// Smitha Dinesh Semwal. `

## Python 3

 `# Python 3 program To calculate  ` `# The Value Of nCr ` ` `  `def` `nCr(n, r): ` ` `  `    ``return` `(fact(n) ``/` `(fact(r)  ` `                ``*` `fact(n ``-` `r))) ` ` `  `# Returns factorial of n ` `def` `fact(n): ` ` `  `    ``res ``=` `1` `     `  `    ``for` `i ``in` `range``(``2``, n``+``1``): ` `        ``res ``=` `res ``*` `i ` `         `  `    ``return` `res ` ` `  `# Driver code ` `n ``=` `5` `r ``=` `3` `print``(``int``(nCr(n, r))) ` ` `  `# This code is contributed ` `# by Smitha `

## C#

 `// C# program To calculate  ` `// The Value Of nCr ` `using` `System; ` ` `  `class` `GFG { ` ` `  `static` `int` `nCr(``int` `n, ``int` `r) ` `{ ` `   ``return` `fact(n) / (fact(r) * ` `                 ``fact(n - r)); ` `} ` ` `  `// Returns factorial of n ` `static` `int` `fact(``int` `n) ` `{ ` `    ``int` `res = 1; ` `    ``for` `(``int` `i = 2; i <= n; i++) ` `        ``res = res * i; ` `    ``return` `res; ` `} ` ` `  `   ``// Driver code ` `   ``public` `static` `void` `Main() ` `   ``{ ` `      ``int` `n = 5, r = 3; ` `      ``Console.Write(nCr(n, r)); ` `   ``} ` `} ` ` `  `// This code is Contributed by nitin mittal. `

## PHP

 ` `

Output:

```10
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.