-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinomialCoefficient.java
More file actions
44 lines (34 loc) · 998 Bytes
/
binomialCoefficient.java
File metadata and controls
44 lines (34 loc) · 998 Bytes
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
/*
# program to find the binomial coefficent
steps:
-> call a binomial coefficient calculating function
-> for calculation, the function will call a factorial calculating function several times
-> at the end, print out the returned result
*/
import java.util.Scanner;
public class Main
{
public static long factorial (int num)
{
long result_1 = 1;
for (int i = num; i >= 1; i--)
{
result_1 = result_1 * i;
}
return result_1;
}
public static long binomialco (int n, int r)
{
long result_2 = factorial (n) / (factorial (r) * factorial (n-r));
return result_2;
}
public static void main (String [] args)
{
Scanner sc = new Scanner (System.in);
System.out.print ("n: ");
int n = sc.nextInt();
System.out.print ("r: ");
int r = sc.nextInt();
System.out.println ("nCr = " + binomialco (n,r));
}
}