Friday, 4 August 2023

C# simple example for recursive call

using System;
class HelloWorld {
  static void Main() {
      var result=print(0);
    Console.WriteLine(result);
  }
  public static string print(int count)
  {
      return print2(count);
  }
  public static string print2(int count)
  {
      if(count <= 2)
      {
          Console.WriteLine(count);
          count++;
          //recursive call upto 3count
          return print(count);
      }
     
      return "Recursive final return";
  }
}

//output like below
0
1
2
Recursive final return

No comments:

Post a Comment