;

C# Program To Print Factorial of a Number Using Recursion


Tutorialsrack 04/08/2019 C# Programs

In this C# program, we will learn how to write a program to print factorial of a number using Recursion.

What is Factorial Number?

In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n:

n!=(n-1)x(n-2)x(n-3)x….3x2x1

The factorial, symbolized by an exclamation mark (!), is a quantity defined for all integers greater than or equal to 0.

For example,

5!=5x4x3x2x1=120

6!=6x5x4x3x2x1=720

The value of 0! is 1.

Here is the code of the program to print factorial of a number using Recursion:

Code - C# Program To Print Factorial of a Number Using Recursion
using System;

namespace TutorialsrackPrograms
{
    class Program
    {
        //c# program to print factorial of a number using Recursion.
        static void Main(string[] args)
        {
            Console.Write("Enter any number: ");
            int number = Convert.ToInt32(Console.ReadLine());
            long fact = Factorial(number);
            Console.WriteLine("Factorial of {0} is {1}", number, fact);
            Console.Read();
        }

        private static long Factorial(int number)
        {
            if (number == 0)
            {
                return 1;
            }
            return number * Factorial(number - 1);
        }
    }
}
Output

Enter any number: 5
Factorial of 5 is 120


Related Posts



Comments

Recent Posts
Tags