0% found this document useful (0 votes)
24 views2 pages

Factorial of A Given Number

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Factorial of A Given Number

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

FACTORIAL OF A GIVEN NUMBER

Source code:
<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>
<h1>Factorial Calculator</h1>
<form method="post" action="">
<label for="number">Enter a number:</label>
<input type="number" id="number" name="number" min="0" required>
<button type="submit">Calculate Factorial</button>
</form>
<?php
function factorial($n) {
// Base case: factorial of 0 or 1 is 1
if ($n == 0 || $n == 1) {
return 1;
} else {
return $n * factorial($n - 1);
}
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$number = intval($_POST['number']); // Get the input number
if ($number >= 0) { // Ensure the number is non-negative
$result = factorial($number);
echo "<p>The factorial of $number is $result.</p>";
} else {
echo "<p>Please enter a non-negative number.</p>";
}
}
?>
</body>
</html>

Output:

You might also like