Home

C# Example: Factorial 2

 

Introduction

This program calculates the factorial of an integer:

using System;

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

    static int Main(string[] args)
    {
	Console.WriteLine("The factorial of 5 is: {0}\n",
		Factorial(5));

        return 0;
    }
}

This would produce:

The factorial of 5 is: 120

Press any key to continue . . .

 

 

 
 

Home Copyright © 2007-2013, FunctionX