Wit 1
Wit 1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>JavaScript Functions in HTML</title>
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
background-color: #f9f9f9;
}
.container {
width: 50%;
margin: 50px auto;
padding: 20px;
background-color: white;
border-radius: 10px;
box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1);
border: 2px solid #333;
}
h1, h3, h5 {
color: #333;
}
</style></head>
<body>
<div class="container">
<h1>Functions in HTML using JavaScript</h1>
<h3>Perform Operations Using JavaScript Functions</h3>
<h5>Sum Operation:</h5>
<p id="a"></p>
<h5>Subtract Operation:</h5>
<p id="b"></p>
<h5>Factorial Operation:</h5>
<p id="f"></p>
<script>
function sum(n1, n2) {
return n1 + n2;
}
function fact(n) {
if (n < 0) return "Factorial of a negative number is not possible";
if (n === 0 || n === 1) return 1;
let result = 1;
for (let i = 2; i <= n; i++) {
result *= i;
}
return result;
}
function natural(n) {
return (n * (n + 1)) / 2;
}
document.getElementById("a").textContent = `Sum of 100 + 200 is:
${sum(100, 200)}`;
document.getElementById("b").textContent = `Subtraction of 400 - 300
is: ${sub(400, 300)}`;
document.getElementById("f").textContent = `Factorial of 5 is:
${fact(5)}`;
document.getElementById("n").textContent = `Sum of the first 50
natural numbers is: ${natural(50)}`;
</script></body></html>
Output: