๐ Introduction
In programming, factorial is a common mathematical function used to solve many problems like permutations, combinations, and recursion practice. In this article, we will learn how to calculate the factorial of a number using recursion in java.
We’ll go step by step and explain everything in simple terms. Whether you're a beginner or just brushing up your Java skills, this article will help you understand recursion easily.
๐ What is a Factorial?
The factorial of a number n is written as n! and is the product of all positive integers from 1 to n.
๐ Formula:
n! = n × (n - 1) × (n - 2) × ... × 1
๐ Examples:
5! = 5 × 4 × 3 × 2 × 1 = 120
3! = 3 × 2 × 1 = 6
1! = 1
0! = 1 (by definition)
๐ค What is Recursion?
Recursion means a function calling itself to solve a smaller version of the same problem.
Simple Example:
Think of recursion like a Russian doll: each doll contains a smaller one inside, and this continues until the smallest one is reached.
๐ Why Use Recursion for Factorial?
The factorial of a number naturally fits recursion because:
-
The factorial of
ndepends onn-1. -
We can break the problem into smaller parts.
For example:
5! = 5 × 4!
4! = 4 × 3!
3! = 3 × 2!
2! = 2 × 1!
1! = 1
๐ป Java Program to Calculate factorial using recursion
Here is the full Java code with step-by-step explanation below.
public class FactorialUsingRecursion {
// Recursive method to calculate factorial
public static int factorial(int n) {
if (n == 0 || n == 1) {
return 1; // Base case: factorial of 0 or 1 is 1
} else {
return n * factorial(n - 1); // Recursive call
}
}
public static void main(String[] args) {
int number = 5; // You can change this number to test
int result = factorial(number);
System.out.println("Factorial of " + number + " is: " + result);
}
}
๐ Step-by-Step Explanation
1. Function Definition
public static int factorial(int n)
-
We define a method named
factorialthat takes an integern. -
It returns an
intvalue which is the factorial ofn.
2. Base Case
if (n == 0 || n == 1) {
return 1;
}
-
This is very important in recursion.
-
Google Advertisement
It stops the recursion from going forever.
-
It tells the function: If
nis 0 or 1, return 1.
3. Recursive Call
return n * factorial(n - 1);
-
This is the heart of recursion.
-
The function calls itself with a smaller number (
n-1) and multiplies it withn.
4. Main Method
int number = 5;
int result = factorial(number);
System.out.println("Factorial of " + number + " is: " + result);
-
We call the recursive function with the number
5. -
It returns
120, which is stored inresultand printed.
๐ง Dry Run (How It Works Internally)
Let’s see what happens when we calculate factorial(4):
factorial(4)
= 4 * factorial(3)
= 4 * (3 * factorial(2))
= 4 * (3 * (2 * factorial(1)))
= 4 * (3 * (2 * 1))
= 4 * 3 * 2 * 1
= 24
โ ๏ธ Important Notes
-
Google Advertisement
Base case is mandatory in recursion, or it will cause a StackOverflowError.
-
For large numbers (like
1000!), recursion may not work due to stack memory limits. -
Use loops for better performance if needed in real-world applications.
โ Output Example
Factorial of 5 is: 120
๐ฆ Practice Tips
-
Try calculating factorial for
0,1,6, and10. -
Try to write the same program using loops and compare both.
-
Understand the call stack behavior in recursion.
๐ Conclusion
In this article, we learned:
-
What a factorial is,
-
What recursion means,
-
How to write a recursive Java method to calculate factorial,
-
And how to think recursively.
Recursion is a powerful concept. Understanding it through examples like factorial is a great way to get started!