In this C# program, we will learn how to write a program to print factorial of a number using Recursion.
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:
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);
}
}
}
Enter any number: 5
Factorial of 5 is 120
Comments