Recursion in C#
Learn how recursion works in C#, understand the base case and recursive case, and solve common programming problems using recursive methods.
Table of Contents
- What is Recursion?
- How Recursion Works
- Base Case and Recursive Case
- Factorial Example
- Print Numbers Example
- Advantages and Disadvantages
- Best Practices
- Summary
What is Recursion?
Recursion is a programming technique where a method calls itself to solve a problem. Each recursive call works on a smaller version of the original problem until a stopping condition is reached.
StackOverflowException.
How Recursion Works
A recursive method has two important parts:
- Base Case – Stops the recursion.
- Recursive Case – Calls the same method with a smaller problem.
Method()
{
if(base condition)
return;
Method();
}
Example 1: Factorial Using Recursion
using System;
class Program
{
static int Factorial(int number)
{
if(number == 0 || number == 1)
return 1;
return number * Factorial(number - 1);
}
static void Main()
{
Console.WriteLine(Factorial(5));
}
}
Output
120
Example 2: Print Numbers from 1 to 5
static void PrintNumbers(int number)
{
if(number > 5)
return;
Console.WriteLine(number);
PrintNumbers(number + 1);
}
PrintNumbers(1);
Output
1
2
3
4
5
Recursion vs Loop
| Recursion | Loop |
|---|---|
| Method calls itself. | Uses for, while or do-while. |
| Uses the call stack. | Uses iteration. |
| Often easier to understand for recursive problems. | Usually faster and uses less memory. |
Advantages of Recursion
- Produces clean and readable code.
- Ideal for tree and graph traversal.
- Useful for divide-and-conquer algorithms.
- Simplifies mathematical problems.
Disadvantages of Recursion
- Consumes more memory due to the call stack.
- Can be slower than loops.
- Incorrect base cases can cause infinite recursion.
StackOverflowException.
Complete Example
using System;
class Program
{
static int Sum(int n)
{
if(n == 1)
return 1;
return n + Sum(n - 1);
}
static void Main()
{
Console.WriteLine(Sum(5));
}
}
Output
15
Best Practices
- Always define a clear base case.
- Ensure each recursive call moves toward the base case.
- Avoid deep recursion when a loop is more efficient.
- Use recursion only when it simplifies the solution.
Summary
| Concept | Description |
|---|---|
| Recursion | A method calling itself. |
| Base Case | Stops recursion. |
| Recursive Case | Calls the same method again. |
| Stack | Stores each recursive method call. |
Interview Questions
- What is recursion?
- Why is the base case important?
- What happens if there is no base case?
- What is the difference between recursion and iteration?
- When should recursion be used?
Practice Exercise
- Write a recursive program to calculate factorial.
- Print numbers from 10 to 1 using recursion.
- Find the sum of the first N natural numbers using recursion.
- Calculate the power of a number using recursion.
FAQ
Is recursion faster than loops?
Not always. Loops are generally faster and use less memory.
Can recursion replace loops?
Yes, many looping problems can be solved recursively, but loops are often more efficient.
Next Tutorial: Classes and Objects in C#
No comments:
Post a Comment