How to find a factorial of a number using PHP
In this PHP tutorial, We will learn how to find the factorial of a number using PHP.
Create an HTML form for the input number.
1 2 3 4 5 6 7 8 9 10 11 12 |
<!DOCTYPE html> <html> <head> <title>Factorial of any number</title> </head> <body> <form name="factorial" action="" method="post"> Number :<input type="text" name="num" value="" required=""><br> <input type="submit" value="Submit" name="submit"> </form> </body> </html> |
PHP Logic for factorial
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php if(isset($_POST['submit'])) { $number = $_POST['num']; /*number to get factorial */ $fact = 1; for($k=1;$k<=$number;++$k) { $fact = $fact*$k; } echo "Factorial of $number is ".$fact; } ?> |
Here is the full code is written for the factorial program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php if(isset($_POST['submit'])) { $number = $_POST['num']; /*number to get factorial */ $fact = 1; for($k=1;$k<=$number;++$k) { $fact = $fact*$k; } echo "Factorial of $number is ".$fact; } ?> <!DOCTYPE html> <html> <head> <title>Factorial of any number</title> </head> <body> <form name="factorial" action="" method="post"> Number :<input type="text" name="num" value="" required=""><br> <input type="submit" value="Submit" name="submit"> </form> </body> </html> |