forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Big_Integer_Factorial.java
26 lines (24 loc) · 1.03 KB
/
Big_Integer_Factorial.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
/*BigInteger class is used for mathematical operation which involves very big integer calculations that are outside the limit of all available primitive data types. For example factorial of 100 contains 158 digits in it so we can't store it in any primitive data type available*/
import java.math.*;
import java.util.*;
class Big_Integer_Factorial {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to find the factorial of");
int n = sc.nextInt();
System.out.println(" The factorial of " + n + " is : " + factorial(n));
}
public static BigInteger factorial(int n) {
BigInteger fact = new BigInteger(Integer.toString(n));
for (int i = 1; i < n; i++)
fact = fact.multiply(BigInteger.valueOf(i));
return fact;
}
}
/* Sample Input and Output :
* Enter the number to find the factorial of
32
The factorial of 32 is : 263130836933693530167218012160000000
Time Complexity : O(n)
Space Complexity : O(1)
*/