Factorial of Large Number in Java
To store the factorial of large number in integer and long datatype is not possible. To solve this problem, BigInteger class from java.math package is used to store the calculated factorial of a large number.
Program Factorial of Large Number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | //Factorial of a Large Number in Java import java.math.BigInteger; import java.util.Scanner; public class largeFactorial{ public static void main(String[] args) { //int number = 30; BigInteger fact = BigInteger.ONE; Scanner sc=new Scanner(System.in); int number; System.out.println("Enter a number:"); number=sc.nextInt(); for(int i = 1; i <= number; i++){ fact = fact.multiply(BigInteger.valueOf(i)); } System.out.println("Factorial of "+ number + " is : " + fact); } } |
Output
Check out our other Java Programming Examples