📖 Scenario: Imagine you want to calculate the factorial of a number, which is the product of all positive integers up to that number. For example, factorial of 4 is 4 x 3 x 2 x 1 = 24.We will use a simple function that calls itself to find the factorial, just like how you might ask a friend to help you with a smaller part of the problem until it becomes very easy.
🎯 Goal: Build a C++ program that calculates the factorial of a given number using recursion.
📋 What You'll Learn
Create a function called factorial that takes an integer parameter n.
Use recursion inside the factorial function to calculate the factorial.
Handle the base case when n is 0 or 1.
Call the factorial function with the number 5 and store the result.
Print the result to the console.
💡 Why This Matters
🌍 Real World
Calculating factorials is useful in math problems, probability, and combinatorics where you count arrangements or combinations.
💼 Career
Understanding recursion is important for many programming jobs, especially those involving algorithms, data structures, and problem solving.
Progress0 / 4 steps
1
Create the factorial function declaration
Write the function declaration for factorial that takes an integer n and returns an integer.
DSA C++
Hint
Start by declaring the function so the compiler knows about it before main.
2
Define the factorial function with base case
Define the factorial function. If n is 0 or 1, return 1 as the base case.
DSA C++
Hint
The base case stops the recursion. Factorial of 0 or 1 is 1.
3
Add recursive call in factorial function
In the factorial function, add a return statement that returns n multiplied by factorial(n - 1).
DSA C++
Hint
Each call multiplies n by the factorial of one less than n.
4
Call factorial and print the result
In main, call factorial(5), store the result in an integer variable called result, and print result using std::cout.
DSA C++
Hint
Store the factorial of 5 in result and print it with std::cout.
Practice
(1/5)
1. What does the factorial of a number represent in mathematics?
easy
A. The product of all positive integers up to that number
B. The sum of all positive integers up to that number
C. The number multiplied by 2
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
Only The product of all positive integers up to that number correctly describes this product, others describe sum or unrelated operations.
Final Answer:
The product of all positive integers up to that number -> Option A
Quick Check:
Factorial = product of integers [OK]
Hint: Factorial multiplies numbers down to 1 [OK]
Common Mistakes:
Confusing factorial with sum
Thinking factorial divides or doubles the number
Mixing factorial with addition
2. Which of the following is the correct base case for a recursive factorial function in C++?
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
By definition, 0! = 1, so the base case must return 1 when n is 0.
Step 2: Check options
Only if (n == 0) return 1; returns 1 when n is 0, others return incorrect values.
Final Answer:
if (n == 0) return 1; -> Option D
Quick Check:
Base case 0! = 1 [OK]
Hint: Base case for factorial is 0! = 1 [OK]
Common Mistakes:
Returning 0 for base case
Using n == 1 but returning wrong value
Missing base case causing infinite recursion
3. What is the output of the following C++ code?
int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}
int main() {
std::cout << factorial(4);
return 0;
}