Monday, 30 September 2019

Calculate the factorial through Recursive call

Calculate the factorial through Recursive call
using System;
namespace ConsoleApplicationSample
{
    class ArrayBCA
    {
#region Recursion
        // So we will calculate the factorial like this.
        //4!=4x(4-1)x(4-2)x(4-3)=24
        //In other words, the Factorial method will call itself by the following code.
        static void Main(string[] args)
        {
            Console.WriteLine("Please Enter a Number...>");
            int number = Convert.ToInt32(Console.ReadLine());
            double factorial = Factorial(number);
            Console.WriteLine("factorial of= " + number + "=" + factorial.ToString());
            Console.ReadKey();
        }
        public static double Factorial(int number)
        {
            if (number == 0)
                return 1;
            return number * Factorial(number - 1);//Recursive call   
        }
        #endregion
    }
}
https://www.youtube.com/channel/UCKLRUr6U5OFeu7FLOpQ-FSw/videos

0 comments

Post a Comment