Recursion in C#

Recursion is a programming technique where a method calls itself to solve a problem. In C#, recursion is commonly used to solve problems that can be divided into smaller subproblems of the same type, like calculating factorials, traversing trees, or implementing algorithms like QuickSort.


How Recursion Works

  1. Base Case: The condition where the recursion stops. Without it, the function would call itself infinitely.
  2. Recursive Case: The part of the function where it calls itself with modified arguments.

  3. using System;

  4. class Program
  5. {
  6.     static int Factorial(int n)
  7.     {
  8.         // Base Case: Stop recursion when n is 0
  9.         if (n == 0)
  10.             return 1;
  11.         
  12.         // Recursive Case: Call Factorial with n - 1
  13.         return n * Factorial(n - 1);
  14.     }

  15.     static void Main()
  16.     {
  17.         int number = 5;
  18.         Console.WriteLine($"Factorial of {number} is: {Factorial(number)}");
  19.     }
  20. }

    How It Works:

    • For Factorial(5), the function works as:
      • 5×Factorial(4)5 \times Factorial(4)
      • 4×Factorial(3)4 \times Factorial(3)
      • 3×Factorial(2)3 \times Factorial(2)
      • 2×Factorial(1)2 \times Factorial(1)
      • 1×Factorial(0)=11 \times Factorial(0) = 1
      • Returns 120.

        Types of Recursion

        1. Direct Recursion:

          • The method directly calls itself.

          • void Example() {
          •     Example(); // Direct call
          • }


          • Indirect Recursion:

            • A method calls another method, which in turn calls the first method.

            • void MethodA() {
            •     MethodB();
            • }

            • void MethodB() {
            •     MethodA();
            • }


              Applications of Recursion

              1. Mathematical Problems: Factorials, Fibonacci series.
              2. Data Structures: Tree and graph traversals.
              3. Algorithms: Divide and conquer, backtracking (e.g., Tower of Hanoi).
              4. Sorting: QuickSort, MergeSort.

              Advantages of Recursion

              • Simplifies code for problems with repetitive substructure.
              • Easier to read and write compared to iterative solutions for some problems.

              Disadvantages of Recursion

              • Can be less efficient due to:
                • High memory usage from the call stack.
                • Risk of stack overflow if the recursion depth is too large.
              • Can sometimes be harder to debug.

              Tips for Writing Recursive Functions

              1. Always define a base case to avoid infinite recursion.
              2. Ensure the recursive call moves towards the base case.
              3. Be cautious about the depth of recursion—consider iterative solutions for deeply recursive problems.

              4. using System;

              5. class Program
              6. {
              7.     static int Fibonacci(int n)
              8.     {
              9.         // Base Case
              10.         if (n <= 1)
              11.             return n;

              12.         // Recursive Case
              13.         return Fibonacci(n - 1) + Fibonacci(n - 2);
              14.     }

              15.     static void Main()
              16.     {
              17.         int terms = 7;
              18.         for (int i = 0; i < terms; i++)
              19.         {
              20.             Console.Write($"{Fibonacci(i)} ");
              21.         }
              22.     }
              23. }



Comments

Post a Comment

Popular posts from this blog