Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to return a value in PHP Factorial Program

Tags:

php

I am working on one of my assignment for Factorial Program. I am struck on one point.

Fact.php

<html>
<body>

<form action="FactCalc.php" method="post">
Enter a Number: <input type="text" name="number">
<input type="submit" Value="Calculate Factorial">
</form>

</body>
</html>

FactCalc.php

<?php
  $num = $_POST["number"];
  $factorial = 1;
  for ($x=$num; $x>=1; $x--)
  {
   $factorial = $factorial * $x;
   }
   echo "Factorial of $num is $factorial";
?> 

this program is running fine. I want to return $factorial to Fact.php and display it there below the text box. I am kind of struck here, can you please help.

like image 583
Bavanari Avatar asked Apr 02 '26 09:04

Bavanari


1 Answers

<?php
if (isset($_POST["number"]))
{
  $_POST["number"] = ($_POST["number"] * 1);
  include "FactCalc.php";
}  
?> 

<form action="FactCalc.php" method="post">      
  <p>
    Enter a Number: 
    <input type="text" name="number">
    <input type="submit" Value="Calculate Factorial"> 

  <?php
  if (isset($factorial))
  {
    echo "</p><p>Factorial of $num is $factorial";
  }
  ?>
  </p>
</form>
like image 137
Jirka Kopřiva Avatar answered Apr 03 '26 22:04

Jirka Kopřiva