Java Program for Factorial of a Number (original) (raw)

Last Updated : 07 Apr, 2025

The factorial of a non-negative integer is multiplication of all integers smaller than or equal to n. In this article, we will learn how to write a program for the factorial of a number in Java.

Formulae for Factorial

n! = n * (n-1) * (n-2) * (n-3) * …….. * 1

Factorial of a Number in Java

**Example:

6! == 6*5*4*3*2*1 = 720.
5! == 5*4*3*2*1 = 120
4! == 4*3*2*1 = 24

Methods to Find the Factorial of a Number

**1. Iterative Solution for Factorial in Java

Below is the implementation of the above method:

Java `

// Java program to find factorial // of given number

// Driver Class 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

The complexity of the above method:

**Time Complexity: O(n)
**Auxiliary Space: O(1)

**2. Factorial in Java Using Recursive Method

Below is the implementation of the above method:

Java `

// Java program to find factorial // of given number

// Driver Class 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

The complexity of the above method:

**Time Complexity: O(n)
**Auxiliary Space: O(n)

**3. One-line Solution (Using the Ternary operator)

Below is the implementation of the above method:

Java `

// Java program to find factorial // of given number

// Driver Class 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

The complexity of the above method:

**Time Complexity: O(n)
**Auxiliary Space: O(n)

In the previous methods, we used basic data types like int or long to calculate factorials. But there’s a problem and the problem is these data types can only store numbers up to a certain size, but what if the number is too big, when the number gets too big they can’t hold the full value, and the result becomes incorrect.

**Note: Java provides a special class called BigInteger to handle large number without worrying about overflow.

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.

Factorial Using BigInteger

Java `

// Java Program to calculate the factorial // of a large number using bigInteger class

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

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!