The factorial of a number is the multiplication of all positive integers from 1 to that number. In Java, factorial programs are commonly used to practice loops, recursion, and arithmetic operations.
Note: Factorial of n is represented as n!.
Formula for Factorial
n! = n * (n-1) * (n-2) * (n-3) * ........ * 1

Example:
6! == 6*5*4*3*2*1 = 720.
5! == 5*4*3*2*1 = 120
4! == 4*3*2*1 = 24
Approaches to Find the Factorial of a Number
1. Iterative Approach
class Test {
// Method to find factorial of given number
static int factorial(int n)
{
int res = 1, i;
for (i = 2; i <= n; i++)
res *= i;
return res;
}
// main method
public static void main(String[] args)
{
int num = 5;
System.out.println("Factorial of " + num + " is "
+ factorial(5));
}
}
Output
Factorial of 5 is 120
Explanation: This program calculates the factorial of a number using an iterative approach by multiplying all integers from 1 to the given number using a loop.
2. Recursive Approach
class Test {
// method to find factorial of given number
static int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
// main method
public static void main(String[] args)
{
int num = 5;
System.out.println("Factorial of " + num + " is "
+ factorial(5));
}
}
Output
Factorial of 5 is 120
Explanation: This program calculates the factorial of a number using recursion by repeatedly multiplying the number with the factorial of its previous number until it reaches 0.
Note: Recursive solutions may cause stack overflow for very large values of n, so iterative or BigInteger approaches are preferred for large inputs.
3. Using the Ternary operator
class Factorial {
// function to find factorial
int factorial(int n)
{
// single line to find factorial
return (n == 1 || n == 0) ? 1
: n * factorial(n - 1);
}
// main function
public static void main(String args[])
{
Factorial obj = new Factorial();
int num = 5;
System.out.println("Factorial of " + num + " is "
+ obj.factorial(num));
}
}
Output
Factorial of 5 is 120
Explanation: This program calculates the factorial of a number using recursion and the ternary operator in a single-line return statement.
Note: The int and long data types cannot store very large factorial values and may cause overflow, to resolve this Java provide the BigInteger class to handle large numbers safely.
Now, we are going to discuss how BigInteger class works in Java, It allows us to calculate factorials of very large number, such as 100! which would overflow the int or long types.
4. Using BigInteger Approach
import java.math.BigInteger;
public class Geeks {
public static BigInteger factorial(int n) {
BigInteger res = BigInteger.ONE;
for (int i = 2; i <= n; i++) {
res = res.multiply(BigInteger.valueOf(i));
}
return res;
}
public static void main(String[] args) {
int num = 100;
System.out.println("Factorial of " + num + " is " + factorial(num));
}
}
Output
Factorial of 100 is 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
Explanation: This program calculates the factorial of a large number using the BigInteger class, which helps prevent overflow when working with values larger than the range of int or long data types.
Please refer factorial of large numbers for a solution that works for large numbers. Please refer complete article on the Program for factorial of a number for more details!