Recursive Factorial method. - CSharp Custom Type

CSharp examples for Custom Type:Method

Description

Recursive Factorial method.

Demo Code

using System;//from www .  j av  a  2  s . c  o m
class FactorialTest
{
   static void Main()
   {
      // calculate the factorials of 0 through 10
      for (long counter = 0; counter <= 10; ++counter)
      {
         Console.WriteLine($"{counter}! = {Factorial(counter)}");
      }
   }
   // recursive declaration of method Factorial
   static long Factorial(long number)
   {
      // base case
      if (number <= 1)
      {
         return 1;
      }
      else // recursion step
      {
         return number * Factorial(number - 1);
      }
   }
}

Result


Related Tutorials