Sponsored Ad

Thursday, August 19, 2010

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

4 comments:

  1. Try this works exceptionally well

    class Program
    {
    static void Main(string[] args)
    {
    Console.WriteLine("Please enter a number whose factorial is to be found: ");
    int number = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("The factorial of the number is : {0}", Factorial(number));
    }
    static long Factorial(long number)
    {
    if (number <= 1)
    return 1;
    else
    return number * Factorial(number - 1);
    }
    }

    }
    ReplyDelete
  2. using System;

    class Factorial
    {
    static void Main()
    {
    Console.Write("Enter a number :");
    int num = int.Parse(Console.ReadLine());

    Console.WriteLine("factorial of {0} is {1}, num, fact(num));
    }

    int fact(int num)
    {
    if(num<=1) return 1;
    return num* fact(num-1);
    }

    }
    ReplyDelete

Sponsored Ad

More Related Articles

Website Update

Followers