Slip no. 21. Write a PHP script to accept the number from user and write a function to calculate the factorial of a given number ( non-negative integer). The function accept the number as argument.
PHP file :
/* Function to get Factorial of a Number */
function getFactorial($num)
                                  $fact = 1;
                                  for($i = 1; $i <= $num ;$i++)
                                  $fact = $fact * $i;
                                 return $fact;
<!doctype html>
<title>Factorial Program using PHP</title>
<form action = "" method="post">
Enter the number whose factorial requires <Br />
<input type="number" name="number" id="number" maxlength="4" autofocus required/>
<input type="submit" name="submit" value="Submit" />
if(isset($_POST['submit']) and $_POST['submit'] == "Submit")
                                 if(isset($_POST['number']) and is_numeric($_POST['number']))
                                  echo 'Factorial Of Number: <strong>'.getFactorial($_POST['number']).'</strong>';

No comments:

Post a comment

Note: only a member of this blog may post a comment.