Tech Study

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

Introduction

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

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

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

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more