-
Notifications
You must be signed in to change notification settings - Fork 1
/
nCr.java
50 lines (41 loc) · 872 Bytes
/
nCr.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package workspace;
public class nCr
{
public static void main(String[] args)
{
java.util.Scanner sc = new java.util.Scanner(System.in);
int n = sc.nextInt();
int r = sc.nextInt();
int res=0;
for(int i=0;i<=r;i++)
{
if(i%2==0)
{
res += nCr(n,i);
}
}
System.out.print(res);
}
static double nCr(int n, int r){
int rfact=1, nfact=1, nrfact=1,temp1 = n-r ,temp2 = r;
if(r>n-r)
{
temp1 =r;
temp2 =n-r;
}
for(int i=1;i<=n;i++)
{
if(i<=temp2)
{
rfact *= i;
nrfact *= i;
}
else if(i<=temp1)
{
nrfact *= i;
}
nfact *= i;
}
return nfact/(double)(rfact*nrfact);
}
}