📖 Scenario: Imagine you have a stack of boxes, and you want to find out how many boxes are in the stack by opening one box at a time and counting. This is similar to how recursion works in programming, where a function calls itself to solve smaller parts of a problem.
🎯 Goal: You will build a simple recursive function in C# that counts down from a number to zero, printing each step. This will help you understand how recursion calls stack up and then unwind.
📋 What You'll Learn
Create a recursive function that prints numbers from a given start down to zero
Use a base case to stop the recursion
Print each number as the function counts down
Understand how the call stack grows and shrinks with recursion
💡 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 is essential for software developers to solve problems that involve repeated tasks or nested data.
Progress0 / 4 steps
1
Create the Main Method and Starting Number
Create a class Program with a Main method. Inside Main, create an integer variable called startNumber and set it to 5.
DSA C#
Hint
Remember to define the class and the Main method exactly as shown. Then create the variable startNumber with the value 5.
2
Add the Recursive Method Signature
Inside the Program class but outside Main, create a static method called CountDown that takes an integer parameter named number.
DSA C#
Hint
Define the method CountDown as static so it can be called from Main. It should accept one integer parameter named number.
3
Implement the Recursive Logic with Base Case
Inside the CountDown method, write an if statement that checks if number is less than 0. If yes, return to stop recursion. Otherwise, print number and call CountDown again with number - 1. Also, call CountDown from Main with startNumber.
DSA C#
Hint
The base case stops recursion when number is less than 0. Otherwise, print the number and call CountDown with one less.
4
Run and Observe the Output
Run the program and observe the output printed to the console. It should print numbers from 5 down to 0, each on its own line.
DSA C#
Hint
The program should print numbers starting from 5 down to 0, each on a new line.
Practice
(1/5)
1. What happens when a recursive function calls itself without a stopping condition?
easy
A. It runs only once and stops
B. It immediately returns without doing anything
C. It causes an infinite loop leading to a stack overflow error
D. It converts into a normal loop automatically
Solution
Step 1: Understand recursion stopping condition
A recursive function must have a base case to stop calling itself; otherwise, it keeps calling endlessly.
Step 2: Effect of missing base case
Without a base case, the function calls stack up until memory runs out, causing a stack overflow error.
Final Answer:
It causes an infinite loop leading to a stack overflow error -> Option C
Quick Check:
Missing base case = infinite recursion [OK]
Hint: Always check for a base case to stop recursion [OK]
Common Mistakes:
Assuming recursion stops automatically
Confusing recursion with loops
Thinking recursion runs only once without base case
2. Which of the following is the correct syntax to define a recursive method in C# that calculates factorial?
easy
A. int Factorial(int n) { if (n == 0) return 1; else return n * Factorial(n - 1); }
B. int Factorial(int n) { while (n > 0) return n * Factorial(n - 1); return 1; }
C. int Factorial(int n) { if (n == 1) return 0; else return n + Factorial(n - 1); }
D. int Factorial(int n) { if (n == 0) return 0; else return n * Factorial(n + 1); }
Solution
Step 1: Check base case correctness
Factorial base case is when n == 0, return 1; this matches int Factorial(int n) { if (n == 0) return 1; else return n * Factorial(n - 1); } only.
Step 2: Check recursive call correctness
Recursive call must reduce n by 1 and multiply; int Factorial(int n) { if (n == 0) return 1; else return n * Factorial(n - 1); } does this correctly.
Final Answer:
int Factorial(int n) { if (n == 0) return 1; else return n * Factorial(n - 1); } -> Option A
Quick Check:
Base case n==0 and n*Factorial(n-1) [OK]
Hint: Base case returns 1; recursive call multiplies by n-1 factorial [OK]