forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBig.java
More file actions
24 lines (21 loc) · 849 Bytes
/
Copy pathBig.java
File metadata and controls
24 lines (21 loc) · 849 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
import java.math.BigInteger;
import java.util.Scanner;
public class Big {
public static void main(String[] args) {
// int startNum = askUserForInteger();
// int endNum = factorial(startNum);
for (int i = 0; i <= 30; i ++) {
factorial(i); // over ~15 these values get wonky because they get too big for the int data type
} // I think BigInteger is mutable because I can construct a new BigInteger and set the attribute
}
public static BigInteger factorial(int x) {
int result = 1;
BigInteger bigResult = BigInteger.valueOf(result);
for (int i = x; i > 0; i--) {
BigInteger counter = BigInteger.valueOf(i);
bigResult = bigResult.multiply(counter);
}
System.out.println(x + "! = " + bigResult);
return bigResult;
}
}