Q:

Write a program in C# Sharp to create a recursive function to find the factorial of a given number

0

Write a program in C# Sharp to create a recursive function to find the factorial of a given number.
Test Data :
Enter a number: 5
Expected Output :
The factorial of 5! is 120

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

using System;
class funcexer11
{
static void Main()
{
      decimal f;
	  Console.Write("\n\nRecursive Function : To find the factorial of a given number :\n");
      Console.Write("------------------------------------------------------------------\n");
	  Console.Write("Input a number : ");
      int num= Convert.ToInt32(Console.ReadLine());
      f = Factorial(num);
      Console.WriteLine("The factorial of {0}! is  {1}", num, f);
}
static decimal Factorial(int n1)
    {
// The bottom of the recursion
      if (n1 == 0)
        {
         return 1;
        }
// Recursive call: the method calls itself
       else
        {
          return n1 * Factorial(n1 - 1);
        }
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now