Find the factorial of a number n in Internet Technologies B.Sc 5th sem
<!DOCTYPE html>
<html>
<head>
<title>Factorial Calculator</title>
</head>
<body>
<h1>Factorial Calculator</h1>
<p>Enter a number: <input type="number" id="numberInput"></p>
<button onclick="calculateFactorial()">Calculate Factorial</button>
<p id="result"></p>
<script>
function calculateFactorial() {
var n = parseInt(document.getElementById("numberInput").value);
if (isNaN(n)) {
document.getElementById("result").innerText = "Please enter a valid number.";
return;
}
if (n < 0) {
document.getElementById("result").innerText = "Factorial is not defined for negative numbers.";
return;
}
var factorial = 1;
for (var i = 1; i <= n; i++) {
factorial *= i;
}
document.getElementById("result").innerText = "Factorial of " + n + " is: " + factorial;
}
</script>
</body>
</html>
Comments
Post a Comment