PHP Tutorial

PHP Examples

PHP Practice

PHP | Factorial of a number

Learn, how to find factorial of a given number using PHP program? By IncludeHelp Last updated : December 22, 2023

Factorial of a number: An Introduction

Factorial of a number is the product of all numbers starting from 1 up to the number itself. Factorial can be calculated for positive integers only. Factorial of 0 is always 1.

  • Factorial of 0 is 0
  • Factorial of 1 is 1
  • Factorial of 2 is 2
  • Factorial of 3 is 6
  • Factorial of 4 is 24
  • Factorial of 5 is 120
  • Factorial of 6 is 720
  • Factorial of 7 is 5040
  • Factorial of 8 is 40320
  • Factorial of 9 is 362880
  • Factorial of 10 is 3628800

Formula to calculate factorial

The formula to calculate the factorial of a number (n) is:
n! = n × (n−1) × (n−2) × ... × 3 × 2 × 1

For example:
5! = 5 × 4 × 3 × 2 × 1 = 120

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

Problem statement

Given a number, you have to write PHP program to calculate the factorial of the given number.

1. Calculating factorial using loop

The simple approach is to calculate factorial using the loop and calculate numbers from 1 to up to the given number.

PHP code to find the factorial of a number using loop

<?php
//program to find factorial of a number
//here, we are designing a function that
//will take number as argument and return
//factorial of that number

//function: getFactorial
function getFactorial($num)
{
    //declare a variable and assign with 1
    $factorial = 1;
    for ($loop = 1; $loop <= $num; $loop++) {
        $factorial = $factorial * $loop;
    }
    return $factorial;
}

//Main code to test above function
$number = 1;
print "Factorial of " . $number . " is = " . getFactorial($number) . "\n";
$number = 2;
print "Factorial of " . $number . " is = " . getFactorial($number) . "\n";
$number = 3;
print "Factorial of " . $number . " is = " . getFactorial($number) . "\n";
$number = 4;
print "Factorial of " . $number . " is = " . getFactorial($number) . "\n";
$number = 5;
print "Factorial of " . $number . " is = " . getFactorial($number) . "\n";
?>

Output

Factorial of 1 is = 1
Factorial of 2 is = 2
Factorial of 3 is = 6
Factorial of 4 is = 24
Factorial of 5 is = 120

2. Calculating factorial using recursion function

Another approach is to use the recursion function to calculate the factorial. In this method, we will call the method itself to get the result.

PHP code to find the factorial of a number using recursion function

<?php
// Recurion function: getFactorial
function getFactorial($num)
{
    if ($num <= 1) {
        return 1;
    } else {
        return $num * getFactorial($num - 1);
    }
}

//Main code to test above function
$number = 1;
print "Factorial of " . $number . " is = " . getFactorial($number) . "\n";
$number = 2;
print "Factorial of " . $number . " is = " . getFactorial($number) . "\n";
$number = 3;
print "Factorial of " . $number . " is = " . getFactorial($number) . "\n";
$number = 4;
print "Factorial of " . $number . " is = " . getFactorial($number) . "\n";
$number = 5;
print "Factorial of " . $number . " is = " . getFactorial($number) . "\n";
?>

Output

Factorial of 1 is = 1
Factorial of 2 is = 2
Factorial of 3 is = 6
Factorial of 4 is = 24
Factorial of 5 is = 120

To understand the above examples,, you should have the basic knowledge of the following PHP topics:

PHP Basic Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.