📖 Scenario: Imagine you want to find the factorial of a number. Factorial means multiplying the number by all smaller numbers down to 1. For example, 4 factorial is 4 x 3 x 2 x 1 = 24.We will use a simple method called recursion, where a function calls itself to solve smaller parts of the problem.
🎯 Goal: Build a program that calculates the factorial of a given number using recursion and prints the result.
📋 What You'll Learn
Create a variable to hold the number to find factorial of
Create a recursive function named factorial that takes one parameter n
Use a base case in the function to stop recursion when n is 1
Use recursion to calculate factorial for numbers greater than 1
Print the factorial of the number
💡 Why This Matters
🌍 Real World
Factorials are used in math, statistics, and computer science to count arrangements and combinations.
💼 Career
Understanding recursion and factorials helps in algorithm design and problem solving in software development.
Progress0 / 4 steps
1
Create the number variable
Create a variable called number and set it to 5.
DSA Python
Hint
Use number = 5 to create the variable.
2
Define the recursive factorial function
Define a function called factorial that takes one parameter n. Inside the function, write an if statement to return 1 if n == 1.
DSA Python
Hint
Use def factorial(n): to start the function and check if n == 1: to return 1.
3
Add recursive call to factorial function
Inside the factorial function, after the if statement, add a return statement that returns n * factorial(n - 1).
DSA Python
Hint
Use return n * factorial(n - 1) to call the function recursively.
4
Print the factorial result
Use print(factorial(number)) to display the factorial of number.
DSA Python
Hint
Use print(factorial(number)) to show the result.
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 with definition
Only The product of all positive integers up to that number correctly states this product definition.
Final Answer:
The product of all positive integers up to that number -> Option A
Quick Check:
Factorial = product sequence [OK]
Hint: Factorial multiplies numbers down to 1 [OK]
Common Mistakes:
Confusing factorial with sum
Thinking factorial doubles the number
Mixing factorial with division
2. Which of the following is the correct base case for a recursive factorial function in Python?
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 base case for factorial
Factorial of 0 is defined as 1, so base case should return 1 when n is 0.
Step 2: Check options for correct base case
if n == 0: return 1 returns 1 when n == 0, which is correct. Others return wrong values.
Final Answer:
if n == 0: return 1 -> Option D
Quick Check:
Base case n=0 returns 1 [OK]
Hint: Base case factorial(0) = 1 [OK]
Common Mistakes:
Returning 0 for factorial(0)
Using wrong base case condition
Returning n+1 instead of 1
3. What is the output of the following code?
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
print(factorial(4))