5. JavaScript Program to find the Area and Perimeter of a Square.
How does this program work?
- In this program you will learn about how to calculate Area and Perimeter of a Square using JavaScript.
- To calculate the perimeter and area of a Square side of the square is required.
- This program performs mathematical calculations and displays output.
Here is the code
<html>
<head>
<title> JavaScript Program to find Area and Perimeter of a Square </title>
</head>
<body>
<table>
<tr>
<td><input type="text" name="a" id="first" placeholder="Enter a number"/> </td>
</tr>
<tr>
<td> <button onclick="square( )">Submit</button> </td>
</tr>
</table>
<div id="num"></div>
<div id="num1"></div>
</body>
<script type="text/javascript">
function square( )
{
var a,area,perimeter;
a = parseInt(document.getElementById("first").value);
area = a*a; //Equation for Area of a square
perimeter = 4*a; //Equation for Perimeter of a square
document.getElementById("num").innerHTML="Area of square is : "+area;
document.getElementById("num1").innerHTML="Perimeter of square is : "+perimeter;
}
</script>
</html>