📖 Scenario: Imagine you are building a simple calculator app that can find the factorial of a number. Factorial means multiplying a number by all the numbers below it down to 1. For example, 4 factorial (written as 4!) is 4 x 3 x 2 x 1 = 24.
🎯 Goal: You will create a Java program that calculates the factorial of a number using a method that calls itself, called recursion. This helps you understand how problems can be solved by breaking them down into smaller parts.
📋 What You'll Learn
Create a method called factorial that takes an integer n and returns the factorial of n using recursion.
Use a base case in the factorial method to stop the recursion when n is 0.
Call the factorial method from the main method with the number 5.
Print the result of the factorial calculation.
💡 Why This Matters
🌍 Real World
Calculating factorials is useful in math problems, probability, and computer science algorithms like permutations and combinations.
💼 Career
Understanding recursion is important for software development roles, especially in algorithm design and problem solving.
Progress0 / 4 steps
1
Create the main class and main method
Create a public class called FactorialCalculator and inside it, create the main method with the exact signature public static void main(String[] args).
DSA Java
Hint
Start by writing the class and main method exactly as shown. This is the basic structure of a Java program.
2
Add the recursive factorial method
Inside the FactorialCalculator class but outside the main method, create a public static method called factorial that takes an integer parameter n and returns an integer. Implement the base case: if n is 0, return 1.
DSA Java
Hint
The base case stops the recursion. When n is 0, factorial is 1.
3
Complete the recursive call in factorial method
In the factorial method, after the base case, return n multiplied by the result of calling factorial with n - 1.
DSA Java
Hint
This line makes the method call itself with a smaller number, building the factorial step by step.
4
Call factorial method and print the result
Inside the main method, call the factorial method with the number 5 and store the result in an integer variable called result. Then print result using System.out.println.
DSA Java
Hint
Factorial of 5 is 120. Make sure to print the result variable.
Practice
(1/5)
1. What does the factorial of a number represent in mathematics?
easy
A. The number multiplied by 2
B. The product of all positive integers up to that number
C. The sum of all positive integers up to that number
D. The number divided by 2
Solution
Step 1: Understand factorial definition
Factorial of a number n (written as n!) is the product of all positive integers from 1 to n.
Step 2: Compare options with definition
Only The product of all positive integers up to that number correctly describes factorial as a product, not sum or division.
Final Answer:
The product of all positive integers up to that number -> Option B
Quick Check:
Factorial = product of integers [OK]
Hint: Factorial multiplies numbers down to 1 [OK]
Common Mistakes:
Confusing factorial with sum
Thinking factorial divides the number
Assuming factorial doubles the number
2. Which of the following is the correct base case for a recursive factorial function in Java?
easy
A. if (n == 1) return n + 1;
B. if (n == 1) return 0;
C. if (n == 0) return 0;
D. if (n == 0) return 1;
Solution
Step 1: Identify factorial base case
Factorial of 0 is defined as 1, so base case returns 1 when n == 0.
Step 2: Check options for correct base case
Only if (n == 0) return 1; returns 1 when n == 0, others return incorrect values.
Final Answer:
if (n == 0) return 1; -> Option D
Quick Check:
Base case n=0 returns 1 [OK]
Hint: Base case for factorial is n=0 returns 1 [OK]
Common Mistakes:
Returning 0 for base case
Using n==1 with wrong return value
Missing base case causing infinite recursion
3. What is the output of the following Java code?
public class Main {
public static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
public static void main(String[] args) {
System.out.println(factorial(4));
}
}