Saturday, July 2, 2016

Factorial de un numero en C#

private static void Main(string[] args)
        {

            int n, factorial = 0;
            string y1 = null;

            do
            {
                Console.WriteLine("Enter the number to find the factorial: ");
                n = Convert.ToInt32(Console.ReadLine());

                if (n < 0)
                {
                    Console.WriteLine("The number does not factorial because it is less than zero. ");
                }
                if (n >= 0)
                {
                    factorial = 1;
                    if (n > 0)
                    {
                        do
                        {
                            factorial = factorial * n;
                            n = n - 1;
                        } while (n > 1);
                    }
                    Console.WriteLine("The factorial of is: " + factorial + ".");
                }
                Console.WriteLine("To continue press " + "y" + ".");
                Console.WriteLine("To exit press a letter.");
                y1 = Console.ReadLine();
            } while (y1 == "Y" || y1 == "y");
            Console.WriteLine("End.");
            Console.ReadLine();
        }