Factorial Program in PHP

Factorial Program in PHP

PHP factorial program; Through this tutorial, we will learn how to write factorial program in PHP using for loop, recursion functions.

Factorial says to multiply all whole numbers from the chosen number down to 1. The factorial symbol is “!”

For examples:

4! = 4 × 3 × 2 × 1 = 24
7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040
Factorial Program In PHP

Factorial program in PHP. Here we would love to share with you how to calculate or find the factorial of any number in PHP.

Here we will take the some of the examples for find or calculate the factorial of any number in PHP like a factorial program in PHP using a loop, factorial program in PHP using recursion, an factorial program in PHP using the form

Factorial Program in PHP

Here we will learn how to calculate factorial of any number. There are two methods to calculate the factorial of any number in PHP.

  • Using loop
    • factorial program in PHP using loop
    • factorial program in PHP using the form
  • Using recursive method
    • factorial program in PHP using recursion

Using loop

Factorial program in PHP using loop

Here we will create a PHP program to find or calculate factorial of number 5 using the for loop in PHP.

<?php
 // define variable and assign value, which you want to find factorial  
 $num = 5;  

 //define variable, which you want to store factorial number value
 $factorial = 1; 

 //iterate the loop 
  for ($i=1; $i <= $num; $i++) {  

	  $factorial = $factorial * $x;  
   } 
   // display the factorial of number
  echo "Factorial of $num is $factorial";  
?>  

Factorial program in PHP using the form

Here we will create a PHP program to find or calculate the factorial of any number using PHP form.

<html>  
<head>  
<title>Factorial using Form in PHP</title>  
</head>  
<body>  
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
    Enter the Number:<br>  
    <input type="number" name="number" id="number">  
    <input type="submit" name="submit" value="Submit" />  
</form>  
<?php   
    if($_POST){  
        $fact = 1;  
        //getting value from input text box 
        $number = $_POST['number'];  
        echo "Factorial of $number:<br><br>";  
        //start loop  
        for ($i = 1; $i <= $number; $i++){         
            $fact = $fact * $i;  
            }  
            echo $fact . "<br>";  
    }  
?>  
</body>  
</html> 

Using recursive method

factorial program in PHP using recursion

Here we will create a PHP program to find or calculate factorial of any number using the recursion in PHP.

<?php  
function factorial($n)  
{  
    if($n <= 1)   
    {  
        return 1;  
    }  
    else   
    {  
        return $n * factorial($n - 1);  
    }  
}  
  
echo "Factorial of 6 is " .factorial(6);  
?>  

Recommended PHP Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *