Sponsored Ad

Thursday, August 19, 2010

Factorial Number Program using For Loop in C#

This program calculate factorial of given number using for loop in C#. for example this program calculate factorial of 4 and give answer 24. you can try this program for different numbers. instead of for loop you can also use other loops like while and do while loop or you can go for recursive functions.

Factorial Number Program using For Loop in C#

 

using System;
using System.Text;
using System.Collections;
using System.Data;
namespace Console_App
{
    public class clsFactorial
    {
        public static void Main()
        {
            try
            {
                Console.WriteLine("The factorial of 4 is: {0}\n",
                    Factorial(4));
            }
            catch(Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();           
        }

        static long Factorial(long number)
        {
            long FinalNumber = 1;
            for (int i = 1; i <= number; i++)
            {
                FinalNumber = FinalNumber * i;
            }
            return FinalNumber;
        }
    }
}

0 comments:

Post a Comment

Sponsored Ad

More Related Articles

Website Update

Followers