Example of a recursive method call.
// Program that shows an example of using a recursive method call.
// A recursive method call is a method that calls itself inside it's own method.
class RecursiveMethodExample
{
public int factorial(int num)
{
int result;
if (num == 1)
{
return 1;
}
else
{
result = factorial(num - 1) * num; // recursive method call here
return result;
}
}
static void Main(string[] args)
{
RecursiveMethodExample n = new RecursiveMethodExample();
// calling the factorial method
Console.WriteLine("Factorial of 1 is : {0}", n.factorial(1));
Console.WriteLine("Factorial of 2 is : {0}", n.factorial(2));
Console.WriteLine("Factorial of 3 is : {0}", n.factorial(3));
Console.WriteLine("Factorial of 4 is : {0}", n.factorial(4));
Console.WriteLine("Factorial of 5 is : {0}", n.factorial(5));
Console.WriteLine("Factorial of 6 is : {0}", n.factorial(6));
Console.WriteLine("Factorial of 7 is : {0}", n.factorial(7));
Console.WriteLine("Factorial of 8 is : {0}", n.factorial(8));
Console.ReadLine();
}
}