beginnerNumber ProblemsJava

Find the Factorial of a Number Using a Loop

Compute the factorial of a non-negative integer using an iterative loop.

Quick Answer

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

Example 1
Input:  5
Output: 120

5! = 5 * 4 * 3 * 2 * 1 = 120.

Example 2
Input:  0
Output: 1

By definition the factorial of 0 is 1; the loop never multiplies anything.

Constraints

  • 0 <= n <= 20 keeps the result within long range
  • Compute 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
Initialize the product to 1, not 0, otherwise every multiplication stays 0.
Hint 2
Multiply the product by each i from 2 up to n; starting at 2 skips a harmless times-one step.
Hint 3
Store the result in a long because factorials overflow int as early as 13!.

Approach

Reveal the step-by-step approach

Multiply a running product by each integer up to n.

  1. Set result = 1.
  2. For i from 2 to n:
    • result = result * i.
  3. 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!.

Time: 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 →
Chat with us