forked from pikachu-17/java-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnCr_and_nPr.java
More file actions
36 lines (36 loc) · 1.1 KB
/
nCr_and_nPr.java
File metadata and controls
36 lines (36 loc) · 1.1 KB
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
import java.util.Scanner;
//not working
public class nCr_and_nPr {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter n and r respectively: ");
int n = scan.nextInt();
int r = scan.nextInt();
// int nfact = 1;
// for(int i=1;i<=n;i++){
// nfact*=i;
// }
// int rfact=1;
// for(int i =1;i<=r;i++){
// rfact*=i;
// }
// int nminusr=n-r;
// int nminusrfact =1;
// for (int i =0;i<=nminusr;i++){
// nminusrfact*=i;
// }
int nfact =factorial(n);
int nminusrfact =factorial((n-r));
int rfact=factorial(r);
int permutation = nfact/nminusrfact;
int combination = nfact/(rfact*nminusrfact);
System.out.println("the permutation for given n and r is: "+permutation+" and combination is: "+combination);
}
static int factorial(int a){
int fact =1;
for(int i =0;i<=a;i++){
fact*=i;
}
return fact;
}
}