Saturday, 9 May 2015

Factorial in C#

In general maths factorial of a number means a result of multiplying a sequence started with the number and end to 1 in descending order like factorial of 5 is 5*4*3*2*1=120.

In C# the code also works on this logic. In our example we are taking number input from the user. You just take a look and try it by yourself.


using System;
namespace Factorial
{
    class Program
    {
        static void Main(string[] args)
        {
            int a,fact=1;
            a = int.Parse(Console.ReadLine());
            for (int i = 1; i <= a; i++)
            {
                fact = fact * i;
            }
            Console.WriteLine("The factorial of "+a+" is "+fact);
            }
    }

}

No comments:

Post a Comment