Sunday, 22 April 2018

Find the factorial in C# using Switch Case


Find the factorial in C# using Switch Case

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class fact
    {
        public static void Main()
        {
            int a, f = 1, ch;
            int[] b = new int[1];
            Console.WriteLine("Enter any number=");
            a = Int32.Parse(Console.ReadLine());
            Console.WriteLine("you can calculate factorial using:");
            Console.WriteLine("1- for loop");
            Console.WriteLine("2- while loop");
            Console.WriteLine("3- do-while loop");
            Console.WriteLine("4- for each loop");

            Console.WriteLine("\nenter your choice");
            ch = Int32.Parse(Console.ReadLine());

            switch (ch)
            {
                case 1: for (int i = 1; i <= a; i++)
                        f = f * i;
                    Console.WriteLine("factorial is=" + f);
                    Console.ReadLine();
                    break;

                case 2: while (a > 0)
                    {
                        f = f * a;
                        a = a - 1;
                    }
                    Console.WriteLine("factorial is=" + f);
                    Console.ReadLine();
                    break;

                case 3: do
                    {
                        f = f * a;
                        a = a - 1;
                    }
                    while (a > 0);
                    Console.WriteLine("the factorial is" + f);
                    Console.ReadLine();
                    break;

                case 4:
                    while (a > 0)
                    {
                        f = f * a;
                        a = a - 1;
                    }
                    b[0] = f;
                    foreach (int i in b)
                        Console.WriteLine("factorial is=" + i);
                    Console.ReadLine();
                    break;
                default: Console.WriteLine("wrong choice");
                    break;
            }
        }
    }

}

https://www.youtube.com/channel/UCKLRUr6U5OFeu7FLOpQ-FSw/videos

0 comments

Post a Comment