Knowledge Guide
HomeDSARecursion

Solution Factorial

Problem Statement

Calculate the Factorial of a Positive Number Using Recursion.

The factorial of a non-negative integer N, denoted as N!, is the product of all positive integers less than or equal to N. The factorial of 0 is defined as 1.

Here is what the example input/output looks like:

InputExpected OutputExplanation
Number = 5Factorial = 120The factorial of 5 is calculated as 5 _ 4 _ 3 _ 2 _ 1 = 120.
Number = 7Factorial = 5040The factorial of 7 is calculated as 7 _ 6 _ 5 _ 4 _ 3 _ 2 _ 1 = 5040.
Number = 1Factorial = 1The factorial of 1 is 1 itself.

Solution

The algorithm to calculate the factorial of a positive number using recursion can be defined as follows:

  1. If the given number is 0 or 1, return 1 (base case).
  2. Otherwise, return the product of the number and the recursive call to calculate the factorial of the number minus 1.

The recursive approach works by breaking down the problem into smaller subproblems. We calculate the factorial of a number by multiplying it with the factorial of the number minus 1. This process continues until the base case is reached, where the number is 0 or 1. The base case stops the recursion and returns 1.

By multiplying the numbers in decreasing order, we ensure that each number is multiplied with the product of the previous numbers, eventually giving us the factorial of the given number.

Example dry run to calculate Factorial of 5
Example dry run to calculate Factorial of 5

Code

Here is the code for this algorithm:

java
public class Solution {

  public static int calculateFactorial(int number) {
    if (number == 0 || number == 1) {
      return 1; // Base case
    }
    return number * calculateFactorial(number - 1); // Recursive call
  }

  public static void main(String[] args) {
    // Example inputs
    int[] examples = { 5, 7, 1 };

    for (int number : examples) {
      int factorial = calculateFactorial(number);
      System.out.println("Factorial of " + number + ": " + factorial);
    }
  }
}

Time and Space Complexity

The time complexity of the algorithm is since we make N recursive calls, where N is the given input.

The space complexity of the algorithm is also because the maximum depth of the recursion is N. Each recursive call adds a new stack frame, consuming additional space on the call stack.

🤖 Don't fully get this? Learn it with Claude

Stuck on Solution Factorial? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **Solution Factorial** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **Solution Factorial** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **Solution Factorial**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **Solution Factorial**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes