Write a C# program to create a recursive function to find the factorial of a given number
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
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..
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