Recursion is a technique where a method calls itself to solve a smaller instance of the same problem, until it reaches a base case that stops the recursion.
class Program
{
// Recursive method to calculate factorial of n
static int Factorial(int n)
{
if (n == 0 || n == 1) // Base case
return 1;
else
return n * Factorial(n - 1); // Recursive call
}
static void Main()
{
int number = 5;
Console.WriteLine($"Factorial of {number} is {Factorial(number)}");
// Output: Factorial of 5 is 120
}
}
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!