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

Term2 Lab Program JavaScript 1

The document contains two JavaScript programs: one for calculating the sum of two numbers using a function and another for finding the area and perimeter of a square and rectangle. Each program includes HTML structure and buttons that trigger the respective calculations through user prompts. The results are displayed using alert boxes.

Uploaded by

smluxurydesign
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)
5 views3 pages

Term2 Lab Program JavaScript 1

The document contains two JavaScript programs: one for calculating the sum of two numbers using a function and another for finding the area and perimeter of a square and rectangle. Each program includes HTML structure and buttons that trigger the respective calculations through user prompts. The results are displayed using alert boxes.

Uploaded by

smluxurydesign
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

Lab Programs

Message Boxes and Different methods to include JS

1. Write a JavaScript program to find the sum of two numbers by calling


a function named sum().
<html>
<head>
<title>To find the sum of two numbers</title>
</head>
<body>
<h1>Sum of two numbers</h1>
<button onclick="sum()">Add</button>
<script>
function sum()
{
var a= prompt("Enter the first number");
a=Number(a);
var b=prompt("Enter the second number");
b=Number(b);
var result=a+b;
alert("The sum ="+result);
}
</script>
</body>
</html>
2. Write a JavaScript program to find the area and perimeter of a Square
and Rectangle.
<html>
<head>
<title>Area and Perimeter of a Square and Rectangle</title>

</head>

<body>
<h1>Find Area and Perimeter of Square and Rectangle</h1>

<h2>Rectangle</h2>
<button onclick="rectangle()">Find Area & Perimeter
of Rectangle</button>
<h2>Square</h2>
<button onclick="square()">Find Area & Perimeter of
Square</button>
<script>
function rectangle()
{
var length = prompt("Enter the length of the rectangle:");
length=Number(length);
var breadth = prompt("Enter the breadth of the rectangle:");
breadth=Number(breadth);
var area = length * breadth;
var perimeter = 2 * (length + breadth);
alert("The area of a square="+area+"and perimeter="+perimeter);

}
function square()
{
var side = prompt("Enter the side of the square:");
side=Number(side);
var area = side * side;
var perimeter = 4 * side;
alert("The area of a square="+area+" and perimeter="+perimeter);

}
</script>

</body>
</html>

You might also like