Q:

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

0

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

All Answers

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

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..

using System;
 
class functionexcercise
{
    static void Main()
    {
        decimal fact;
        Console.Write("Enter a number : ");
        int num = Convert.ToInt32(Console.ReadLine());
        fact = Factorial(num);
        Console.WriteLine("The factorial of number {0} is  {1}", num, fact);
        Console.ReadLine();
    }
    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);
 
 
        }
 
    }
 
}

Result:

Enter a number : 5

The factorial of number 5 is  120

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

total answers (1)

<< Write a C# program to Print Binary Equivalent of a...