Cross_Column

Wednesday, 1 July 2026

Recursion in C#



Recursion in C# | Beginner Tutorial

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

  1. What is Recursion?
  2. How Recursion Works
  3. Base Case and Recursive Case
  4. Factorial Example
  5. Print Numbers Example
  6. Advantages and Disadvantages
  7. Best Practices
  8. 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.

Note: Every recursive method must have a base case. Without it, the recursion will never stop and will eventually cause a 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
Tip: Recursion is often used for problems involving trees, directories, graphs, mathematical calculations, and divide-and-conquer algorithms.

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.
Common Mistake: Forgetting the base case results in infinite recursion and eventually throws a 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

ConceptDescription
RecursionA method calling itself.
Base CaseStops recursion.
Recursive CaseCalls the same method again.
StackStores each recursive method call.

Interview Questions

  1. What is recursion?
  2. Why is the base case important?
  3. What happens if there is no base case?
  4. What is the difference between recursion and iteration?
  5. 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

Few More

Recursion in C#

Recursion in C# | Beginner Tutorial Recursion in C# Learn how recursion works in C#, understand the base case and re...