Find the Factorial of a Number Using a Loop
Compute the factorial of a non-negative integer using an iterative loop.
Start a running product at 1 and multiply it by every integer from 2 up to n. When the loop ends, the product is n!. Use a long to hold the result because factorials grow very fast and overflow int quickly. By definition 0! and 1! are both 1.
Problem Statement
The factorial of a non-negative integer n, written n!, is the product of
all integers from 1 to n. By definition 0! = 1. So 5! = 5 * 4 * 3 * 2 * 1 = 120.
Given n, compute n! using a loop (not recursion). Because factorials grow very
quickly, accumulate the answer in a long so it holds larger results than an int.
Input: A single non-negative integer n.
Output: The factorial n! as a long.
Examples
Input: 5
Output: 1205! = 5 * 4 * 3 * 2 * 1 = 120.
Input: 0
Output: 1By definition the factorial of 0 is 1; the loop never multiplies anything.
Constraints
0 <= n <= 20 keeps the result within long rangeCompute iteratively, not recursively
Think Before You Code
Reveal the questions to ask yourself first
- What value should the running product start at so the first multiplication is correct?
- Why does 0! equal 1 even though there is nothing to multiply?
- Why prefer a long over an int for the accumulator?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Multiply a running product by each integer up to n.
- Set
result = 1. - For
ifrom 2 ton:result = result * i.
- Return
result.
Starting result at 1 makes 0! and 1! come out as 1 automatically, because
the loop body runs zero times.
Dry Run
Walk through the example step by step
Computing 5!:
i | result before | result after (result * i)
--+---------------+--------------------------
2 | 1 | 2
3 | 2 | 6
4 | 6 | 24
5 | 24 | 120
Final result: 120
Solution
Reveal the full Java solution
public class FactorialLoop {
public static long factorial(int n) {
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
public static void main(String[] args) {
System.out.println(factorial(5)); // 120
System.out.println(factorial(0)); // 1
}
}
The accumulator starts at 1 so the identity 0! = 1! = 1 falls out for free when
the loop does not execute. Using long roughly doubles the range you can compute
before overflow compared to int, though even long tops out around 20!.
O(n)Space: O(1)Common Mistakes
- Initializing the product to 0, which forces the result to stay 0 forever.
- Using int for the result, which overflows silently starting at 13!.
Edge Cases to Test
- 0! and 1! must both return 1 (the loop runs zero times).
- Large n such as 21 overflows even a long and needs BigInteger.
Interview Follow-Ups
- How would you compute the same factorial with recursion?
- How would you handle arbitrarily large factorials using BigInteger?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →