n!

        /// <summary>
        /// Calculate n! by 2 different ways
        ///
        /// n! = n x (n – 1)!,  0! = 1
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            Console.WriteLine(IterateFact(5));
            Console.WriteLine(RecursiveFact(5));
            Console.ReadKey();
        }
 
        private static int IterateFact(int n)
        {
            if (n == 0) return 1;
            int temp = 1;
            while (n > 0)
            {
                temp = n * temp;
                n–;
            }
            return temp;
        }
 
        private static int RecursiveFact(int n)
        {
            if (n == 0) return 1;
            return n * RecursiveFact(n – 1);
        }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s