📖 Scenario: Imagine you have a stack of boxes, and you want to count how many boxes are in the stack by taking one box off at a time until none are left.
🎯 Goal: You will write a recursive function in C# that counts down from a number to zero using a base case and a recursive case.
📋 What You'll Learn
Create an integer variable called number with the value 5
Create a recursive method called CountDown that takes an integer parameter n
In CountDown, write a base case that stops recursion when n is 0
In CountDown, write a recursive case that calls CountDown with n - 1
Print each number from n down to 1 during the recursive calls
Call CountDown with the variable number
Print Done! after the recursion finishes
💡 Why This Matters
🌍 Real World
Recursion is used in many real-world problems like searching files in folders, solving puzzles, and processing tree structures.
💼 Career
Understanding recursion helps in software development roles that involve algorithms, data processing, and problem solving.
Progress0 / 4 steps
1
Create the starting number
Create an integer variable called number and set it to 5.
DSA C#
Hint
Use int number = 5; to create the variable.
2
Define the recursive method
Define a recursive method called CountDown that takes an integer parameter n.
DSA C#
Hint
Use void CountDown(int n) to define the method.
3
Add base case and recursive case
Inside CountDown, add a base case that returns when n is 0. Otherwise, print n and call CountDown with n - 1.
DSA C#
Hint
The base case stops recursion when n is 0. The recursive case prints n and calls CountDown(n - 1).
4
Call the method and print done
Call CountDown with the variable number and then print Done!.
DSA C#
Hint
Call CountDown(number); and then Console.WriteLine("Done!");.
Practice
(1/5)
1. What is the purpose of the base case in a recursive function?
easy
A. To print the result of the recursion
B. To call the function again with a smaller input
C. To stop the recursion and prevent infinite calls
D. To increase the input size for the next call
Solution
Step 1: Understand the role of base case
The base case is the condition where the recursion stops to avoid infinite calls.
Step 2: Differentiate base case from recursive case
The recursive case calls the function again, but the base case returns a value without further calls.
Final Answer:
To stop the recursion and prevent infinite calls -> Option C
Quick Check:
Base case = stop recursion [OK]
Hint: Base case stops recursion to avoid endless loops [OK]
Common Mistakes:
Confusing base case with recursive case
Thinking base case calls function again
Ignoring base case leads to infinite recursion
2. Which of the following is the correct syntax for a base case in this C# recursive function to calculate factorial?
int Factorial(int n) {
if (___) return 1;
return n * Factorial(n - 1);
}
easy
A. n == 0
B. n > 0
C. n < 0
D. n == 1
Solution
Step 1: Identify the base case for factorial
Factorial of 0 is defined as 1, so base case is when n == 0.
Step 2: Check options for correct base case condition
Only 'n == 0' correctly stops recursion and returns 1.
Final Answer:
n == 0 -> Option A
Quick Check:
Factorial base case = n == 0 [OK]
Hint: Factorial base case is n equals zero [OK]
Common Mistakes:
Using n > 0 as base case causes infinite recursion
Using n == 1 misses factorial(0) definition
Using n < 0 causes wrong behavior
3. What will be the output of this C# code?
int Sum(int n) {
if (n == 0) return 0;
return n + Sum(n - 1);
}
Console.WriteLine(Sum(3));