PHP Practical Slip 7 Q.B

Write a PHP script to calculate the factorial of a number using a function.

<?php
// Function to calculate factorial
function factorial($n) {
    $fact = 1;
    for ($i = 1; $i <= $n; $i++) {
        $fact *= $i;
    }
    return $fact;
}

$number = 5;
$result = factorial($number);

// Display result
echo "Number: " . $number . "<br>";
echo "Factorial: " . $result;
?>
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top