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

The factorial of a number n is denoted as n!.

Examples

0! = 1
1! = 1
2! = 2 \\cdot 1 = 2
3! = 3 \\cdot 2 \\cdot 1 = 6
4! = 4 \\cdot 3 \\cdot 2 \\cdot 1 = 24
5! = 5 \\cdot 4 \\cdot 3 \\cdot 2 \\cdot 1 = 120
6! = 6 \\cdot 5 \\cdot 4 \\cdot 3 \\cdot 2 \\cdot 1 = 720
7! = 7 \\cdot 6 \\cdot 5 \\cdot 4 \\cdot 3 \\cdot 2 \\cdot 1 = 5040
8! = 8 \\cdot 7 \\cdot 6 \\cdot 5 \\cdot 4 \\cdot 3 \\cdot 2 \\cdot 1 = 40320
9! = 9 \\cdot 8 \\cdot 7 \\cdot 6 \\cdot 5 \\cdot 4 \\cdot 3 \\cdot 2 \\cdot 1 = 362880
10! = 10 \\cdot9 \\cdot 8 \\cdot 7 \\cdot 6 \\cdot 5 \\cdot 4 \\cdot 3 \\cdot 2 \\cdot 1= 3628800

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
     * @author Thomas (www.adamjak.net)
     */
 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
  * @author Thomas (www.adamjak.net)
  */
 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
  * @author Thomas (www.adamjak.net)
  */
 
 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))))))
 







       
 

Place for your banner

Here is the position ready for our customer's banners.