BigInteger in java

Abhishek Raj
2 min readAug 4, 2021
Factorial

If you are interested in coding or math,one famous question you often heard is “ find the factorial of n numbers ”.And you did this question many times but still if someone ask you to find the factorial of some big number like n=100 or n=1000. If you find the factorial of n=100 in the same way as earlier you have find , it may exceed the limit of integer or long.Can you guess how to find the factorial of such a large number like n=100.To handle that problem there is an in-built class of java -BigInteger class.

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. We can store as large Integer as we want in it. There is no theoretical limit on the upper bound of the range because memory is allocated dynamically but practically as memory is limited you can store a number which has Integer.

Java program to find large factorials using BigInteger
import java.math.BigInteger;
import java.util.Scanner;

public class Example
{

static BigInteger factorial(int N)
{
BigInteger f = new BigInteger(“1”);
for (int i = 2; i <= N; i++)
f = f.multiply(BigInteger.valueOf(i));

return f;
}
public static void main(String args[]) throws Exception
{
int N = 20;
System.out.println(factorial(N));
}
}

--

--