Friday, July 15, 2022

Factorial of number C# ASP.NET Recursion

 using System;

namespace FactorialExample

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("Enter a number");

            int number = Convert.ToInt32(Console.ReadLine());

            long fact = GetFactorial(number);

            Console.WriteLine("{0} factor

ial is {1}", number, fact);           

            Console.ReadKey();

        }

 

        private static long GetFactorial(int number)

        {          

            if (number == 0)

            {

                return 1;

            }

            return number * GetFactorial(number-1);

        }

    }

}

No comments:

Post a Comment