The following are the common definitions of Binomial Coefficients.
A binomial coefficient C(n, k) can be defined as the coefficient of x^k in the expansion of (1 + x)^n. 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.
Problem Statement: Write a function that takes two parameters n and k and returns the value of Binomial Coefficient C(n, k). For example, your function should return 6 for n = 4 and k = 2, and it should return 10 for n = 5 and k = 2.
Approach: Below are the steps to be followed in order to create a function that returns the binomial coefficient of two integers.
- Create a function that takes to arguments n and k.
- Now check whether the n and k are numbers. If both or one of them are not numbers then return NaN.
- Now check if k is less than 0 or k is greater than n. If one of the statements is true then return 0.
- Now check if the k is equal to 1 or k is equal to n. If one of the statements is true then return 1.
- Now check if k is equal to 1 or k is equal to (n-1). If one of the statements is true then return n.
- Now write the logic to get the binomial coefficient.
Example: This example shows the above-explained approach.