Factorial in PHP
Q. Write a PHP program to find the factorial of a number
<?php
// Define the number
$number = 5;
$factorial = 1;
// Calculate factorial using loop
if ($number < 0) {
echo "Factorial is not defined for negative numbers.";
} else {
for ($i = 1; $i <= $number; $i++) {
$factorial *= $i;
}
echo "Factorial of $number is: $factorial";
}
?>
Q. Write an algorithm to find the factorial of a number in PHP
1. Start
2. Define an integer number
3. If number < 0, print "Factorial not defined" and stop
4. Set factorial = 1
5. Loop from i = 1 to number:
a. Multiply factorial = factorial * i
6. Print factorial
7. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com