Recursive function in action : Function Definition « Language Basics « C# / C Sharp






Recursive function in action

Recursive function in action

using System;

public class FactorialTest
{
   public static void Main( string[] args )
   {
      for ( long counter = 0; counter <= 10; counter++ )
         Console.WriteLine( "{0}! = {1}",
            counter, Factorial( counter ) );
   }

   public static long Factorial( long number )
   {
      if ( number <= 1 )
         return 1;
      else
         return number * Factorial( number - 1 );
   }
}

           
       








Related examples in the same category

1.Recursive sum methodRecursive sum method
2.Use a recursive method, travel, to journey from start to finishUse a recursive method, travel, to journey from start to finish
3.Recursive Factorial method.
4.Define functionDefine function
5.Catch StackOverflowException for recursive function