Site icon T4Tutorials.com

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.

  1. Flowchart of PHP program to find the factorial of a number.
  2. PHP program to find the factorial of a number.
  3. PHP program to find the factorial of a number with form.
  4. PHP program to find the factorial of a number with form and database.

PHP program to find the factorial of a number.

Figure: factorial flowchart with program

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.

<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.

<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.

<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.

Figure: factorial program in PHP with database and form.
Exit mobile version