Factorial using C#.NET

using System;
using System.Text;
using System.Collections;
using System.Data;
namespace Console_Factorial
{
    public class clsFactorial
    {
        public static void Main()
        {
            Console.WriteLine("Enter the number to find Factorial");
            int n = int.Parse(Console.ReadLine().ToString());
            try
            {
                Console.WriteLine("The factorial of "+n+" is: {0}\n",
                    Factorial(n));
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadLine();
        }

        static long Factorial(long number)
        {
            if (number <= 1)
                return 1;
            else
                return number * Factorial(number - 1);
        }
    }
}

For this program you will get the output as follows
Enter the number to find Factorial
5
The factorial of 5 is: 120

Comments

Popular Posts