-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactBigInteger.java
More file actions
46 lines (35 loc) · 1.18 KB
/
FactBigInteger.java
File metadata and controls
46 lines (35 loc) · 1.18 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
37
38
39
40
41
42
43
44
45
46
import java.math.BigInteger;
import java.util.Scanner;
public class FactBigInteger{
public static BigInteger fact(int n) {
if (n < 0) {
return BigInteger.valueOf(-1); // Handle invalid input (negative values) as needed.
}
if (n == 0 || n == 1) {
return BigInteger.valueOf(1); // Base case: 0! and 1! = 1
}
BigInteger result = BigInteger.valueOf(1);
for (int i = 2; i <= n; i++) {
result = result.multiply(BigInteger.valueOf(i));
}
return result;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n;
while (true) {
System.out.print("Enter an integer to compute factorial (enter 0 to exit): ");
n = scanner.nextInt();
if (n == 0) {
break;
}
BigInteger factorial = fact(n);
if (factorial.equals(BigInteger.valueOf(-1))) {
System.out.println("Invalid input. Please enter a non-negative integer.");
} else {
System.out.println(factorial);
}
}
scanner.close();
}
}