-
Notifications
You must be signed in to change notification settings - Fork 1
/
Combination.java
61 lines (45 loc) · 952 Bytes
/
Combination.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
51
52
53
54
55
56
57
58
59
60
61
import java.io.*;
public class Combination
{
static InputStreamReader r;
static BufferedReader input;
int n,k;
Combination()throws IOException
{
r=new InputStreamReader(System.in);
input=new BufferedReader(r);
n=0;
k=0;
drive();
}
void drive()throws IOException
{
String s;
System.out.println("Enter the desired permutation in the form nCk... n,r=integers");
s=input.readLine();
extract(s);
double result;
result=combination();
printResult(result);
}
double combination()throws IOException
{
Factorial f=new Factorial();
double a=f.Factorial(n);
double b=f.Factorial(n-k);
double c=f.Factorial(k);
double d=a/(b*c);
return d;
}
void extract(String s)
{
int pointer=s.indexOf('C');
int length=s.length();
n=Integer.parseInt(s.substring(0,pointer));
k=Integer.parseInt(s.substring(pointer+1,length));
}
void printResult(double result)
{
System.out.println(n+"C"+k+"="+result);
}
}