Table of contents
1.
Introduction
2.
What is a factorial program in Java?
3.
Factorial Methods in Java
4.
1.) Factorial Program using iteration in Java
4.1.
Algorithm:
4.2.
Code:
5.
Java
5.1.
Output: 
6.
Complexity Analysis
6.1.
Time Complexity:
6.2.
Space Complexity:
7.
2.) Factorial Program using recursion in Java
7.1.
Algorithm:
7.2.
Code:
8.
Java
8.1.
Output: 
9.
Complexity Analysis
9.1.
Time Complexity:
9.2.
Space Complexity:
10.
3.) Factorial Program using Loop in Java
10.1.
Java
11.
4.) Find Factorial of a number using BigInteger
11.1.
Java
12.
5.) Factorial Java Program Using Ternary Operator
12.1.
Java
13.
Frequently Asked Questions
13.1.
How to use factorial in Java?
13.2.
How do you solve the factorial of 4?
13.3.
Can we find a factorial of a negative number?
13.4.
What is the time complexity of the factorial program?
13.5.
What is the space complexity of the factorial program?
13.6.
What is an example of a factorial Program?
14.
Conclusion
Last Updated: Sep 23, 2025
Easy

Factorial of a Number in Java Explained

Author Aniket Majhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Factorial of a Number in Java refers to calculating the product of all positive integers up to a given number using Java programming. This concept is commonly implemented using loops or recursion, making it a fundamental exercise in control structures, methods, and mathematical logic in Java. Understanding factorial logic strengthens core algorithmic thinking.

Factorial of a Number in Java

What is a factorial program in Java?

A factorial program in Java is a Java-based implementation used to calculate the factorial of a number, which is the product of all positive integers less than or equal to that number. The program typically uses loops or recursion to perform the calculation. It helps beginners understand iteration, recursion, and method logic in Java.

Factorial of a number(non-negative) is the multiplication of all smaller integers than the number, including the number itself.
We can write factorial of a number as,
 

Factorial Methods in Java

Factorial methods in Java are different approaches to calculate the factorial of a number, such as using iteration, recursion, loops, or BigInteger for large values.

  1. Factorial Program using iteration in Java
  2. Factorial Program using recursion in Java
  3. Factorial Program using Loop in Java
  4. Find the Factorial of a number using BigInteger
  5. Factorial Java Program Using Ternary Operator

1.) Factorial Program using iteration in Java

The iteration method is discussed below:

Algorithm:

The steps for calculating factorial using iteration in java are as follows - 

  • Initialise the result to 1
long Result = 1;
  • Run a loop in the range [1, number]
for(int i = 1 ; i <= Number ; i++) {}
  • In each iteration update the result.
Result = Result * i;
  • Finally, print the result
System.out.printf("The Factorial of %d is: %d ",Number,Result);

Code:

  • Java

Java

public class Factorial {
   public static void main(String[] args) {
       int Number = 6; // Given Number
       long Result = 1;
       for(int i = 1 ; i <= Number ; i++) {
           Result = Result * i;
       }

       System.out.printf("The Factorial of %d is: %d ",Number,Result);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:
 

From the above program, the factorial of 6 comes out to be 720.

Let’s see the steps for number 6 in iteration.

Complexity Analysis

The time and space complexity of the above code is shown below:

Time Complexity:

The time complexity of the above code is O(n), where n is the number.

Space Complexity:

The space complexity of the above code is O(1), as we are not allocating any extra spaces.

2.) Factorial Program using recursion in Java

The Recursion method is discussed below:

Algorithm:

The steps for calculating factorial using recursion in java are as follows:

  • Create a recursive function by giving the Number as a parameter.
public static long fact(int Number) {}
  • If the number is 0, return 1 else call the function for  Number - 1
if(Number == 0) return 1;
else return Number * fact(Number - 1);
  • Store the result in a variable after the end of the recursion.
long Result = fact(Number);
  • Print the result.
System.out.printf("The factorial of Number %d is: %d",Number,Result);

Code:

  • Java

Java

public class Factorial {
   public static long fact(int Number) {
       if (Number == 0)
           return 1;
       else
           return Number * fact(Number - 1);
   }

   public static void main(String[] args) {
       int Number = 6;
       long Result = fact(Number);
       System.out.printf("The factorial of Number %d is: %d", Number, Result);
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:
 

Let's see the steps for number 6 in recursion.
 

Try it by yourself on Java Online Compiler.

Complexity Analysis

The time and space complexity of the above code is shown below:

Time Complexity:

The time complexity of the above code is O(n), where n is the number.

Space Complexity:

The space complexity of the above code is O(n) as a stack of size n will be allocated here to store the functions’ state.

Also check out Addition of Two Numbers in Java here.

3.) Factorial Program using Loop in Java

The factorial of a number nnn is the product of all positive integers less than or equal to nnn. It is denoted as n!n!n!. This can be computed using a loop by iterating from 1 to nnn and multiplying the result at each step.

Here's an example of a factorial program using a loop in Java:

  • Java

Java

public class FactorialUsingLoop {
public static void main(String[] args) {
int number = 5; // Example number
long factorial = 1;

for (int i = 1; i <= number; i++) {
factorial *= i;
}

System.out.println("Factorial of " + number + " is: " + factorial);
}
}
You can also try this code with Online Java Compiler
Run Code

 

Output

Factorial of 5 is: 120

 

In this code, we initialize factorial to 1. We use a for loop to iterate from 1 to the given number. During each iteration, we multiply factorial by the current loop variable. Finally, we print the result.

4.) Find Factorial of a number using BigInteger

For very large numbers, the factorial result can exceed the range of primitive data types like int or long. In such cases, we use BigInteger from the java.math package, which can handle arbitrarily large integers.

Here's an example of finding the factorial of a number using BigInteger:

  • Java

Java

import java.math.BigInteger;

public class FactorialUsingBigInteger {
public static void main(String[] args) {
int number = 100; // Example large number
BigInteger factorial = BigInteger.ONE;

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

System.out.println("Factorial of " + number + " is: " + factorial);
}
}
You can also try this code with Online Java Compiler
Run Code

 

Output

Factorial of 100 is: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

 

In this code, we initialize factorial to BigInteger.ONE. We use a for loop to iterate from 1 to the given number. During each iteration, we multiply factorial by the current loop variable i converted to BigInteger using BigInteger.valueOf(i). Finally, we print the result.

5.) Factorial Java Program Using Ternary Operator

This method uses recursion combined with the ternary operator (? :) to compute the factorial in a compact form. The ternary operator simplifies conditional logic by handling the base and recursive cases in one line.

Code Example:

  • Java

Java

public class FactorialTernary {
static int factorial(int n) {
return (n == 0 || n == 1) ? 1 : n * factorial(n - 1);
}

public static void main(String[] args) {
int number = 5;
int result = factorial(number);
System.out.println("Factorial of " + number + " is: " + result);
}
}
You can also try this code with Online Java Compiler
Run Code

Output:

Factorial of 5 is: 120 

This program demonstrates a concise way to calculate the factorial of a number using recursion and the ternary operator in Java.

Frequently Asked Questions

How to use factorial in Java?

To use factorial in Java, define a method using loops or recursion to multiply integers up to a given number and return the result.

How do you solve the factorial of 4?

4! = 4 * 3 * 2 * 1 = 24

Can we find a factorial of a negative number?

No factorial of a negative number is not possible.
let’s  understand why
We can write n! = (n + 1)! / (n + 1).
Like , 5! = 6! / 6 , 4! = 5! / 5.
But for negative numbers like -1.
-1! = 0! / 0
It is undefined. This applies to other negative numbers as well.

What is the time complexity of the factorial program?

For both the iteration and recursion solution,  the time complexity of the factorial program is O(n), where n = number.

What is the space complexity of the factorial program?

For the iteration solution, the space complexity is O(1), and for the recursion solution, the space complexity is O(n) due to the call stack.(n = Number). For factorial n, a stack of size n will be allocated to store the functions' state.

What is an example of a factorial Program?

A factorial program calculates the product of all positive integers up to a given number nnn. In Java, using a loop or BigInteger for large numbers ensures accurate computation. Example: computing 5! = 5 * 4 * 3 * 2 * 1 = 120.

Conclusion

In this article, we have extensively discussed the factorial program and its implementations in Java.

In this blog, we started with the basic definition of factorial. Then we discussed the two possible methods, i.e., iterative and recursive approaches regarding the factorial program their implementations with the proper program flow diagram.

Recommended Readings:

Recommended Problems:

Live masterclass