-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFactComp.java
More file actions
49 lines (44 loc) · 982 Bytes
/
Copy pathFactComp.java
File metadata and controls
49 lines (44 loc) · 982 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
45
46
47
48
49
import java.math.BigInteger;
public class FactComp{
public static void main(String[] args){
System.out.println(factorial(12));
System.out.println();
System.out.println(factorial(20L));
System.out.println();
System.out.println(factorial(170.0));
System.out.println();
System.out.println(factorial(new BigInteger("170")));
System.out.println();
System.out.println(factorial(25.0));
System.out.println();
System.out.println(factorial(new BigInteger("25")));
}
static int factorial(int n){
if(n==0){
return 1;
}else{
return n*factorial(n-1);
}
}
static long factorial(long n){
if(n==0){
return 1;
}else{
return n*factorial(n-1);
}
}
static double factorial(double n){
if(n == 1.0){
return 1.0;
}else{
return n*factorial(n-1);
}
}
static BigInteger factorial(BigInteger n){
if(n.equals(BigInteger.ZERO)){
return BigInteger.ONE;
}else{
return n.multiply(factorial(n.subtract(BigInteger.ONE)));
}
}
}