Java Program to find Factorial of a Number using BigInteger - The Coding Shala
Home >> Java Programs >> Factorial Program using BigInteger
Other Posts You May Like
In this post, we will learn how to find Factorial of a Number using BigInteger in Java.
Java Program to find Factorial of a Number using BigInteger
We use int or long to store the result of factorial but long is also not enough to store the value of factorial of larger numbers, let's say n = 100 so in Java, we use BigInteger to store the factorial of large numbers.
Example 1:
Input: 30
Output: 265252859812191058636308480000000
Java Program:
// Java program to Find Factorial using BigInteger import java.util.Scanner; import java.math.BigInteger; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // take number input System.out.println("Enter the number"); BigInteger num = sc.nextBigInteger(); BigInteger fact = BigInteger.ONE; BigInteger i = BigInteger.ONE; // calculate factorial while(i.compareTo(num) <= 0) { fact = fact.multiply(i); i = i.add(BigInteger.ONE); } System.out.println("Factorial of " + num + " is: " + fact); sc.close(); } }
Output:
Enter the number 30 Factorial of 30 is: 265252859812191058636308480000000
Other Posts You May Like
- Java Program to find the Factorial of a Number using Loop
- Java Program to find the Factorial of a Number using Recursion
- Java Program to Find the Area of Triangle
- Java Program to Find the Area of Circle
- Java Program to Find the Area of Rectangle
Comments
Post a Comment