In this tutorial, I am going to write a program for how to find Factorial of Given Number in C#.

After completing this tutorial you will understand:

  • How to find Factorial of a given number in C#.

How to find Factorial of a Given Number in C#?

Open Visual Studio and Create a Console Application in C# called FactorialNumberDemo.

Factorial

Open Program.cs file and write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace FactorialNumberDemo
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter a number");
int iNumber = Convert.ToInt32(Console.ReadLine());
long lFact = GetFactorial(iNumber);
Console.WriteLine("{0} factorial is {1}", iNumber, lFact);
Console.ReadKey();
}
private static long GetFactorial(int iNumber)
{
if (iNumber == 0)
{
return 1;
}
return iNumber * GetFactorial(iNumber - 1);
}
}
}

Run the application and look over the output window.

Factorial

Conclusion:

Hope you understand how to find Factorial of a given number in C#. Your valuable comments, Suggestions and Feedbacks are always welcome.

View More:

Thank You.

Leave a Comment