Calculate the factorial of a number

These while loops will calculate the Factorial of a number:

Visual Basic / QBasic

Dim counter as Integer : counter = 5
Dim factorial as Long : factorial = 1
While (counter > 0)
factorial = factorial * counter     'Multiply
counter = counter - 1               'Decrement
Wend
Print factorial                       'Prints out the result.

REALbasic

Dim counter as Integer = 5
Dim factorial as Integer = 1
While counter > 0
factorial = factorial * counter     //Multiply
counter = counter - 1               //Decrement
Wend
MsgBox Str( factorial )               // Prints out the result.

C or C

unsigned int counter = 5;
unsigned long factorial = 1;
while (counter > 0)
factorial *= counter--; /*Multiply, then decrement.*/

printf("%i", factorial);

Recursive approach in C or C

unsigned long factorial(unsigned long f)
{
if (f) return(f * factorial(f - 1))
return 1;
}

printf("%i", factorial(5));

Perl

<HIGHLIGHTSYNTAX language="perl">

sub factorial {
my \$n = shift;
my \$f = 1;
\$f *= \$n-- while \$n > 0;    # Multiply, then decrement
return \$f;
}
sub recursive_factorial {
my \$n = shift;
return \$n * recursive_factorial(\$n - 1) if \$n;
return 1;
}
print factorial 5;
print recursive_factorial 5;

</HIGHLIGHTSYNTAX>

Tcl (Tool command language)

set counter 5
set factorial 1
while {\$counter > 0} {
set factorial [expr \$factorial * \$counter]
incr counter -1
}
puts \$factorial
proc factorial n {
if {\$n <= 0} {
return 1
} else {
return [expr {\$n * [factorial [expr {\$n - 1}]]}]
}
}

Java, Csharp

The code for the loop is the same for Java and Csharp:

Note: if you intend to use the program to generate large factorials (such as 20!, 50! etc.) then you should store and return the factorial as a double rather than a long or int, due to the far greater size of double-precision values.

int counter = 5;
long factorial = counter;
while (counter > 1)
factorial *= --counter; // Multiply the decremented number.

//!n = n*(n-1)

For Java the result is printed as follows:

System.out.println(factorial);

The equivalent in C# is:

System.Console.WriteLine(factorial);

Rather than using a loop, use a recursive method:

/**
* A recursive factorial calculation method
*
* @param n the number to be factored
* @return the factorial of n
*/
public static int factorial(int n) {
return ((n == 0) ? 1 : n * factorial(n - 1));
}

OCaml

Example 1

# let rec fact = function
| 0 -> 1
| n -> n * fact(n-1);;
val fact : int -> int = <fun>

For example:

# fact 5;;
- : int = 120

Example 2

# let rec f n = if n=0 then 1 else n*f(n-1);;
val f : int -> int = <fun>

For example:

# f 5;;
- : int = 120

Pascal

program Factorial
var
Counter, Factorial: integer;
begin
Counter := 5;
Factorial := 1;
while Counter > 0 do begin
Factorial := Factorial * Counter;
Counter := Counter - 1;
end;
Write(Factorial);
end.

Python

import operator
def factorial(num):
return reduce(operator.mul, range(1, num   1))

Scheme

(define (fac n)
(if (= n 0)
1
(* n (fac (- n 1)))))
;give fac a value of 5
(fac 5)

Common Lisp

;;; This is tail recursive. Remember that tail-recursion optimization isn't
;;; enabled by default in most CL compilers and interpreters
(defun ! (n)
(labels
((fact1 (n m)
(if (zerop n)
m
(fact1 (1- n) (* m n)))))
(fact1 n 1)))

This is an optimized algorithm, you can find more about it and some other ways to increase its performance at .

(defun ! (n)
(let ((shift 0))
(labels ((fact1 (n m)
(cond
((and (evenp n) (> m 1))
(incf shift (ash n -1))
(fact1 (ash n -1) (ash m -1)))
((<= n m) n)
(t (* (fact1 n (ash m 1)) (fact1 (- n m)(ash m 1)))))))
(ash (fact1 n 1) shift))))