Factorial of Large Number in Java

It is beyond the realm of imagination to expect to store factorial for a huge number like 50 into inbuilt information types like a whole number or long.

Since factorial of 50 has just about 60 digits. Envision how we can store it in int or long.

We can discover factorial of such numbers utilizing BigInteger class characterized in java. math bundle.

Beneath program shows how you can do this.

Program

import java.math.BigInteger;
import java.util.Scanner;
 
class BigFactorial {
	public static void main(String arg[]){
		BigInteger fac=new BigInteger("1");
		int n;
		
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter a number:");
		n=sc.nextInt();
		
		for(int i=2;i<=n;++i){
			fac=fac.multiply(BigInteger.valueOf(i));
		}
		
		System.out.println(fac);
	}
}

Output

Factorial of Large Number in Java

Leave a Comment

error: Alert: Content is protected!!