<?php
//=====================================================================================================
// factorial: 0! = 1,   n! = n * (n-1)!
//     args: the number n
//   return: the factorial number
//=====================================================================================================
error_reporting(0);


$num = $_GET['num'];                                        //get number from user

if (! $num) $num = 10;                                      //if no number, set it to 10

print "<h2>Factorial of $num </h2>";

$factor = factorial($num);                                  //call the factorial function
print number_format($factor);                               //print final result

print "<br><br><br> Number can also be passed at end of URL?num=x ";

//==================================================================================
function factorial($number)
{
        if ($number < 0)
        {
            print "Error - negative number cannot have a factorial \n";
            exit(1);
        }

        if ($number == 0)
            return 1;
        else
        {
            $fact   = factorial($number -1);                    //recursively call factorial()
            $result = $number * $fact;
            return($result);
                
          //or  return ($number * factorial($number - 1));      //all on 1 line
        } 
}
//==================================================================================
?>

<?php include "../include.php"; ?>              <!-- hyperlink to see the code -->