﻿ Factorial

The factorial is a function of a non-negative integer , whose output is a pruduct of all positive integers less or equal than . The factorial of 0 is defined as 1.

The factorial of a number is denoted as .

## Code

    /**
* Returns the factorial of a number n using iteration
* @param number number n >= 0
* @return the factorial of a number n
*/
public static int factorial(int number) {
if (number < 0) throw new IllegalArgumentException("Negative argument");
int result = 1;
for (int i = number; i > 1; i--) {
result *= i;
}
return result;
}
/**
* Returns the factorial of a number n using recursion
* @param number number n >= 0
* @return the factorial of a number n
*/
public static int factorialRec(int number) {
if (number < 0) throw new IllegalArgumentException("Negative argument");
if (number == 0 || number == 1) return 1;
return number * factorialRec(number - 1);
}

    /**
* Returns the factorial of a number n using iteration
* @param number number n >= 0
* @return the factorial of a number n
*/
int fakt(int number) {
if (number < 0)
{
return 0;
}

int i, result = 1;

for (i = number; i > 1; i--)
{
result *= i;
}
return result;
}

/**
* Returns the factorial of a number n using recursion
* @param number number n >= 0
* @return the factorial of a number n
*/
int fakt_rek(int number) {
if (number < 0)
{
return 0;
}

if (number == 0 || number == 1)
{
return 1;
}

return number * fakt_rek(number - 1);
}

 /**
* Calculates the factorial of a number n using iteration
* @param $number number n >= 0 * @return the factorial of a number n * @author Thomas (www.adamjak.net) */ function factorial($number) {
if ($number < 0) { die("Negative argument"); }$result = 1;
for ($i =$number; $i > 1;$i--) {
$result *=$i;
}
return $result; } /** * Calculates the factorial of a number n using recursion * @param$number number n >= 0
* @return the factorial of a number n
*/

function factorial_rec($number) { if ($number < 0) {
die("Negative argument");
}
if ($number == 0 ||$number == 1) {
return 1;
}
return $number * factorial_rec($number - 1);
}

 fact(0,1).
fact(N,R) :- N >= 0, integer(N), N1 is N - 1, fact(N1,R1), R is N * R1.

 (defun fact (x)
"generates factorial of x"
(if (= x 0)
1
(* x (fact (- x 1)))))

(defun fact2 (x)
"generates factorial of x"
(if (zerop x)
1
(* x (fact2 (1- x)))))

(defun fact3 (x)
"generates factorial of x"
(cond ((zerop x) 1)
(T (* x (fact3 (1- x))))))