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;
        }
    }
}

Factorial Number Program using C# Recursion Function

This is simple program to calculate factorial number of a given number using recursion functions. This program is full written in C# and supported by every version. You can also use loops instead of recursion functions to calculate factorial number.

Please feel free to contact us if you feel any problem while implementing recursion logic.

Factorial Number Program using C# Recursion Function

Source Code:

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 6 is: {0}\n",
                    Factorial(6));
            }
            catch(Exception ex)
            {
                //handle exception here
            }
            Console.ReadLine();           
        }

        static long Factorial(long number)
        {
            if (number <= 1)
                return 1;
            else
                return number * Factorial(number - 1);
        }  
    }
}

Sponsored Ad



More Related Articles

Website Update

Recent Posts

Followers