-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinomialCoef.java
33 lines (29 loc) · 1014 Bytes
/
BinomialCoef.java
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
/*
* Filename : BinomialCoef.java
* Problem Statement:
* Computing Binomial Coefficient
* -------------------------------------------------------------------
* AUTHOR : GANESH PAI, Dept. of CS&E, NMAMIT, Nitte
* YEAR : 2021
* E-mail : [email protected]
* ------------------------------------------------------------------------------
*/
import java.util.*;
public class BinomialCoef {
// Returns value of Binomial Coefficient C(n, k)
static int binomialCoeff(int n, int k)
{
if (k > n)
return 0;
if (k == 0 || k == n)
return 1;
return binomialCoeff(n - 1, k - 1) + binomialCoeff(n - 1, k);
}
public static void main(String[] args)
{
System.out.println("Enter the value of n & k: ");
int n = new Scanner(System.in).nextInt();
int k = new Scanner(System.in).nextInt();
System.out.printf("C(%d, %d) = %d\n", n, k, binomialCoeff(n, k));
}
}