3. JavaScript Program to find the Square and Cube of a given number.
How does this program work?
- In this program you will learn about how to find the Square and Cube of a given number using JavaScript.
- This program will helpful to calculate square and cube of a number.
- And then by using the given formula you will get output.
Here is the code
<html>
<head>
<title>JavaScript Program to find Square and Cube of a number</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,Square,Cube;
a = parseInt(document.getElementById ("first").value);
//Formulae for Square and Cube
Square = a*a;
Cube = a*a*a;
document.getElementById ("num").innerHTML ="Square of "+a+" is : "+Square;
document.getElementById ("num1").innerHTML ="Cube of "+a+" is : " +Cube;
}
</script>
</html>