PHP program to find the factorial of a number with form and database, flowchart
PHP program to find the factorial of a number with form and database
In this tutorial, we will learn about the followings.
- Flowchart of PHP program to find the factorial of a number.
- PHP program to find the factorial of a number.
- PHP program to find the factorial of a number with form.
- PHP program to find the factorial of a number with form and database.
PHP program to find the factorial of a number.
PHP program to find the factorial of a number.
In this example, programmer stores the values in the variables. However, if you interested to see the program about taking values in the form, please see example 2.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<html> <body> <?php $p = 3; $f = 1; for ($x=$p; $x>=1; $x--) { $f = $f * $x; } echo "Factorial of number is $f"; ?> </body> </html> |
PHP program to find the factorial of a number with form.
In this example, we will take the inputs from the user and store them in variables.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<html> <body> <form method="post"> <input type="text" name="name"/> <input type="submit" name="OK" /> </form> <?php if(isset($_POST['OK'])){ $p =$_POST['name']; $f = 1; for ($x=1; $x<=$p; $x++) { $f = $f * $x; } echo "Factorial of number is $f "; } ?> </body> </html> |
PHP program to find the factorial of a number with a database.
In this example, we will take the inputs from the user and temporarily store them in variables and then finally stores them permanently in the database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<html> <body> <form method="post"> <input type="text" name="name"/> <input type="submit" name="OK" /> </form> <?php $con=mysqli_connect("localhost","root","","test"); if(isset($_POST['OK'])){ $p =$_POST['name']; $f = 1; for ($x=1; $x<=$p; $x++) { $f = $f * $x; } echo "Factorial of number is $f "; $sql=mysqli_query($con,"INSERT INTO Factorial (Number,Factorial) VALUES ('$p','$f')"); } ?> </body> </html> |
Database to find the factorial of a number.