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>