Factorial program in C#

 

Factorial program in C#

Factorial Program in C#: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:

4! = 4*3*2*1 = 24    

6! = 6*5*4*3*2*1 = 720    

 


C# code

   private static void Factorial()

        {

            int i, fact = 1, number;

            Console.Write("Enter any Number: ");

            number = int.Parse(Console.ReadLine());

            for (i = 1; i <= number; i++)

            {

                fact = fact * i;

            }

            Console.Write("Factorial of " + number + " is: " + fact);

        }


output



Factorial using recursive function


        static void Main(string[] args)

        {

            Console.Write("Enter a Number : ");

            int number = int.Parse(Console.ReadLine());

            long factorial = RecursiveFactorial(number);

            Console.Write($"Factorial of {number} is: {factorial}");

 

            Console.ReadLine();

        }

        static long RecursiveFactorial(int number)

        {

            if (number == 1)

            {

                return 1;

            }

            else

            {

                return number * RecursiveFactorial(number - 1);

            }

        }




Share this

Related Posts

Previous
Next Post »