- Expand
5*4*3*2*1
Find Factorial in Java
What you’ll learn
- The definition of
n!forn >= 0, including0! = 1. - A recursive solution and an iterative solution in Java.
- Overflow limits for
long, input validation, and live preview.
Prerequisites
Methods, loops, recursion basics, and Java integer types.
- Know that
0! = 1and factorial is for nonnegative integers. - Use
longfor exact values only up to20!.
Formal definition
0! = 1, and for n >= 1, n! = n * (n - 1)!.
5!5 * 4 * 3 * 2 * 1 = 120
Intuition
- Rule
n * (n - 1)!
Takeaway: each step multiplies the previous factorial by the next integer, so values grow very fast.
Live preview
Compute exact factorial values for 0..20 (safe for Java long).
Algorithm
Goal: compute n! for nonnegative n.
Base case
If n <= 1, return 1.
Multiply down
Use recursion n * factorial(n-1) or loop from 2 to n.
📜 Pseudocode
function factorial(n): // assume n >= 0
if n <= 1:
return 1
return n * factorial(n - 1)Recursive factorial
Classic recursive approach with validation and long bound.
public class Main {
static final int FACT_MAX_LONG = 20;
static long factorialRecursive(int n) {
if (n == 0 || n == 1) return 1L;
return n * factorialRecursive(n - 1);
}
public static void main(String[] args) {
int number = 5;
if (number < 0) {
System.out.println("Factorial is not defined for negative integers.");
return;
}
if (number > FACT_MAX_LONG) {
System.out.println("n too large for exact long in this demo (max " + FACT_MAX_LONG + ").");
return;
}
long result = factorialRecursive(number);
System.out.println("Factorial of " + number + " is: " + result);
}
}Factorial of 5 is: 120
Iterative factorial
Same result with O(1) extra space.
public class Main {
static final int FACT_MAX_LONG = 20;
static long factorialIterative(int n) {
long result = 1L;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
public static void main(String[] args) {
int number = 5;
if (number < 0) {
System.out.println("Factorial is not defined for negative integers.");
return;
}
if (number > FACT_MAX_LONG) {
System.out.println("n too large for exact long in this demo (max " + FACT_MAX_LONG + ").");
return;
}
System.out.println("Factorial of " + number + " is: " + factorialIterative(number));
}
}Factorial of 5 is: 120
Optimization and scaling
For large n: use BigInteger in Java because long overflows after 20!.
Iterative approach: avoids recursion stack growth while keeping the same O(n) work.
Interview tip: always mention input validation and overflow constraints.
🔄 Input / output examples
| n | n! |
|---|---|
0 | 1 |
1 | 1 |
5 | 120 |
20 | 2432902008176640000 |
Edge cases and pitfalls
n < 0
Factorial is undefined for negative integers in this tutorial context.
n > 20
Exact value does not fit in Java long; use BigInteger.
Deep recursion
Recursive calls consume stack frames; iterative version is safer for bigger inputs.
⏱️ Time and space complexity
| Version | Time | Extra space |
|---|---|---|
| Recursive | O(n) | O(n) |
| Iterative | O(n) | O(1) |
Summary
- Definition:
0! = 1,n! = n * (n - 1)!. - Code choices: recursion is intuitive, iteration uses constant extra space.
- Limit: Java
longstores exact factorial values only through20!.
❓ FAQ
Stirling's approximation describes factorial growth: n! ~ sqrt(2*pi*n) * (n/e)^n. It helps estimate very large factorials when exact fixed-width integers overflow.
8 people found this page helpful
