📖 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.
🎯 Goal: You will write a recursive function in C that calculates the factorial of a number using a base case and a recursive case.
📋 What You'll Learn
Create a function called factorial that takes an integer n as input.
Use a base case where if n is 1, the function returns 1.
Use a recursive case where the function returns n multiplied by factorial(n - 1).
Call the factorial function with the value 5 and print the result.
💡 Why This Matters
🌍 Real World
Recursion is used in many places like calculating factorials, navigating file systems, and solving puzzles.
💼 Career
Understanding recursion helps in solving problems that require breaking down tasks into smaller similar tasks, common in software development and algorithm design.
Progress0 / 4 steps
1
Create the factorial function with base case
Write a function called factorial that takes an integer n and returns 1 if n is 1.
DSA C
Hint
The base case stops the recursion. If n is 1, return 1.
2
Add the recursive case to factorial function
Inside the factorial function, add a recursive case that returns n * factorial(n - 1) when n is not 1.
DSA C
Hint
The recursive case calls factorial again with n - 1 and multiplies the result by n.
3
Call factorial function with 5
In the main function, call factorial with the value 5 and store the result in an integer variable called result.
DSA C
Hint
Use int result = factorial(5); to call the function and save the answer.
4
Print the factorial result
Add a printf statement in main to print the value of result with the message: "Factorial of 5 is: %d\n".
DSA C
Hint
Use printf("Factorial of 5 is: %d\n", result); to show the answer.
Practice
(1/5)
1. What is the purpose of the base case in a recursive function?
easy
A. To call the function again with a smaller input
B. To stop the recursion and prevent infinite calls
C. To increase the input size for the next call
D. To print the result of the recursion
Solution
Step 1: Understand what recursion does
Recursion calls the same function repeatedly with smaller inputs.
Step 2: Identify the role of the base case
The base case stops the recursion by providing a condition where no further calls happen.
Final Answer:
To stop the recursion and prevent infinite calls -> Option B
Quick Check:
Base case = stop recursion [OK]
Hint: Base case stops recursion to avoid infinite loops [OK]
Common Mistakes:
Confusing base case with recursive case
Thinking base case calls function again
Ignoring the need for a stopping condition
2. Which of the following is the correct syntax for a base case in a recursive function in C?
easy
A. if (n == 0) return 1;
B. while (n > 0) callFunction(n-1);
C. for (int i=0; i
D. return callFunction(n+1);
Solution
Step 1: Identify base case syntax
The base case uses an if condition to check when to stop recursion.
Step 2: Check each option
if (n == 0) return 1; uses if (n == 0) return 1; which correctly stops recursion. Others use loops or increase n, which are incorrect.
Final Answer:
if (n == 0) return 1; -> Option A
Quick Check:
Base case uses if condition [OK]
Hint: Base case uses if condition to stop recursion [OK]
Common Mistakes:
Using loops instead of base case
Increasing input instead of decreasing
Missing return statement in base case
3. What is the output of the following C code?
int factorial(int n) {
if (n == 0) return 1;
else return n * factorial(n - 1);
}
int main() {
printf("%d", factorial(3));
return 0;
}