[go: up one dir, main page]

0% found this document useful (0 votes)
1 views3 pages

Wit 1

This document presents an HTML program that demonstrates the use of JavaScript functions for basic arithmetic operations. It includes functions for sum, subtraction, factorial, and calculating the sum of the first N natural numbers, displaying the results on a web page. The program is styled for a clean and centered layout using CSS.

Uploaded by

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

Wit 1

This document presents an HTML program that demonstrates the use of JavaScript functions for basic arithmetic operations. It includes functions for sum, subtraction, factorial, and calculating the sum of the first N natural numbers, displaying the results on a web page. The program is styled for a clean and centered layout using CSS.

Uploaded by

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

Experiment –05

Aim : A HTML program which demonstrates the use of functions in java


script.

<!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>

<h5>Sum of First N Natural Numbers:</h5>


<p id="n"></p>
</div>

<script>
function sum(n1, n2) {
return n1 + n2;
}

function sub(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:

You might also like