Make a factorial calculator in C# without multiplication sign


Guys, today I came up with another very interesting post. Though this is a class assignment for an intermediate level students but most of developers ran into this problem and don’t find a way out. So here is a simple post for them with source code and guidance.

A factorial calculator will do some basic maths like this: 5! = 5x4x3x2x1 = 120

However, basic problem is we are NOT suppose to use the multiplication sign while calculating the factorial results. So here is the code. Remember! This is a console application – Make a new console app and add the following code in your console app.

Download factorial Calculator in C# without using multiplication sign source code here

  static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Enter the digit to find factorial");
                int FactorialDigit = int.Parse(Console.ReadLine());

                List<int> ListOfFactorialResults = new List<int>();
                ListOfFactorialResults.Add(Multiplication(FactorialDigit, --FactorialDigit));

                int i = 1;
                while (FactorialDigit -1 > 1)
                {
                    ListOfFactorialResults.Add(Multiplication(ListOfFactorialResults[i-1], --FactorialDigit));
                    i++;
                }

                Console.WriteLine(string.Format("Factorial of this number is : " + "{0}",
                        ListOfFactorialResults[ListOfFactorialResults.Count -1]));
                Console.ReadKey();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        public static int Multiplication(int FirstDigit, int SecondDigit)
        {
            try
            {
                int i = 0, product = 0;

                while (i < FirstDigit)
                {
                    product = product + SecondDigit;
                    i++;
                }
                return product;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

I hope it will help students and some developers too ;-)

Regards,
Muneeb

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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

%d bloggers like this: