How to calculate factorial of a number in PHP. In this tutorial, I am going to discuss factorial program in PHP using recursion as well as iterative approach.
Before solving this problem let’s understand what is factorial?
What is Factorial?
Factorial of a non-negative integer n (n!), is the product of all positive integers less than or equal to n.
For example –
5! = 5 * 4 * 3 * 2 * 1 = 120
4! = 4 * 3 * 2 * 1 = 24
Fibonacci series program in PHP
If you are not familiar with the concept of recursion. Then check my previous tutorial on recursion Vs iteration.
Factorial Program in PHP
Let’s write a function to calculate factorial of a number in PHP. In this code example, I have created a function calculateFactorial which takes a number as an argument and calculate it’s factorial using for loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
/* Print factorial of a number * @param integer $num */ function calculateFactorial($num) { $fact = 1; /* Factorial of zero is 1 */ if ( $num == 0 ) { echo 1; } else if ( $num > 0 ) { for ( $i = 1; $i <= $num; $i++) { $fact = $fact * $i; } echo $fact." \n"; } else if ( $num < 1 ) { echo " Factorial of a negative number does not exist "; } } calculateFactorial (5); calculateFactorial (10); /* Output * 120 * 3628800 */ |
PHP Program to Find Factorial of a Number using Recursion
We have learned, how to calculate factorial of a number using iterative approach. Let’s write a PHP code to calculate factorial of a number using recursion.
Recursion concept with an example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * Print Factorial of a Number in PHP using Recursion */ function calculateFactorial($num) { /* Factorial of zero is 1 */ if ( $num == 0 ) { return 1; } else if ( $num > 0 ) { //Recursive call return $num * calculateFactorial ($num - 1); } } $result = calculateFactorial (5); echo $result; |
Explanation
This is how this program is executed.
1 2 3 4 5 |
return 5 * (calculateFactorial(4)); return 5 * (4 * calculateFactorial(3)); return 5 * (4 * (3 * calculateFactorial(2))); return 5 * (4 * (3 * (2 * calculateFactorial(1)))); return 5 * (4 * (3 * (2 * (1)))); |
We can also do the same thing by creating a class.Let’s create a PHP class which calculate and print factorial of a number.
PHP Class to Calculate and Print Factorial of a Number
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
/* * A class to calculate factorial of a number */ class Factorial { private $num; private $fact = 1; public function __construct($num) { $this->num = $num; } public function calculateFactorial() { if ( !empty($this->num) ) { for ($i = 1; $i <= $this->num; $i++) { $this->fact = $this->fact * $i; } } else if ( $this->num < 0 ) { $this->fact = 0; } return $this->fact; } } /* Instance of a factorial class is created */ $fact = new Factorial(10); /* Calculate factorial method is called */ echo $fact->calculateFactorial(); |