Factorial in Java

Learn to calculate factorial using loops, recursion, and mathematical concepts

❗ Factorial in Java

Factorial of a number n (written as n!) is the product of all positive integers from 1 to n. For example, 5! = 5×4×3×2×1 = 120. Java can calculate factorials using loops or recursion efficiently.


// Calculate factorial using loop
int factorial = 1;
for (int i = 1; i <= n; i++) {
    factorial *= i;
}
System.out.println(factorial);
                                    

Different Approaches

🔁

For Loop

Iterative approach using for loop

int factorial = 1;
for (int i = 1; i <= n; i++) {
    factorial *= i;
}
return factorial;
🔄

While Loop

Alternative iterative method

int factorial = 1, i = 1;
while (i <= n) {
    factorial *= i;
    i++;
}
return factorial;
🎯

Recursive Method

Function calling itself

public static int factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}
📊

BigInteger

For very large factorials

BigInteger factorial = BigInteger.ONE;
for (int i = 1; i <= n; i++) {
    factorial = factorial.multiply(
        BigInteger.valueOf(i));
}

🔹 Complete Example Program

Here's a complete Java program to calculate factorial:

import java.util.Scanner;
import java.math.BigInteger;

public class Factorial {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.print("Enter a number: ");
        int number = scanner.nextInt();
        
        if (number < 0) {
            System.out.println("Factorial is not defined for negative numbers.");
        } else {
            // Using iterative method
            long iterativeResult = factorialIterative(number);
            System.out.println(number + "! = " + iterativeResult + " (Iterative)");
            
            // Using recursive method
            long recursiveResult = factorialRecursive(number);
            System.out.println(number + "! = " + recursiveResult + " (Recursive)");
            
            // Using BigInteger for large numbers
            BigInteger bigResult = factorialBig(number);
            System.out.println(number + "! = " + bigResult + " (BigInteger)");
        }
        
        scanner.close();
    }
    
    // Iterative method
    public static long factorialIterative(int n) {
        long factorial = 1;
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
        return factorial;
    }
    
    // Recursive method
    public static long factorialRecursive(int n) {
        if (n <= 1) {
            return 1;
        }
        return n * factorialRecursive(n - 1);
    }
    
    // BigInteger method for large numbers
    public static BigInteger factorialBig(int n) {
        BigInteger factorial = BigInteger.ONE;
        for (int i = 1; i <= n; i++) {
            factorial = factorial.multiply(BigInteger.valueOf(i));
        }
        return factorial;
    }
}

Sample Output:

Enter a number: 5

5! = 120 (Iterative)

5! = 120 (Recursive)

5! = 120 (BigInteger)

🔹 Understanding Factorial

Factorial Examples:

  • 0! = 1 (by definition)
  • 1! = 1
  • 2! = 2×1 = 2
  • 3! = 3×2×1 = 6
  • 4! = 4×3×2×1 = 24
  • 5! = 5×4×3×2×1 = 120

Method Comparison:

  • Iterative: More memory efficient, faster for large numbers
  • Recursive: More elegant code, but uses more memory (stack)
  • BigInteger: Can handle very large factorials (100!, 1000!, etc.)
  • Limitation: long can only handle up to 20! accurately

🧠 Test Your Knowledge

What is the value of 4! (4 factorial)?