Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

211421205125

SQUARE AND CUBE OF A NUMBER

AIM:
To write a JavaScript program that calculates the Square and Cube of a given number.

ALGORITHM:
Step 1: Start the process.

Step 2: Display a message "Enter a number".

Step 3: Read and store the user input as 'number'.

Step 4: Calculate the square of number.

Step 5: Calculate the cube of number.

Step 6: Display the square and cube results.

Step 7: Stop the process.

PROGRAM:
<!DOCTYPE html>
<html>
<head>
<title>Square and Cube Calculator</title>
</head>
<body>
<h1>Square and Cube Calculator</h1>
<p>Enter a number:</p>
<input type="number" id="numberInput" placeholder="Enter a number">
<br><br>
<button onclick="calculateSquare()">Calculate Square</button>
<button onclick="calculateCube()">Calculate Cube</button>
<br><br>
<p>Square: <span id="squareResult"></span></p>
<p>Cube: <span id="cubeResult"></span></p>
<script>
211421205125

function calculateSquare() {
const number = parseFloat(document.getElementById("numberInput").value);
if (!isNaN(number)) {
const square = number * number;
document.getElementById("squareResult").textContent = square;
} else {
document.getElementById("squareResult").textContent = "Invalid input";
}
}
function calculateCube() {
const number = parseFloat(document.getElementById("numberInput").value);
if (!isNaN(number)) {
const cube = number * number * number;
document.getElementById("cubeResult").textContent = cube;
} else {
document.getElementById("cubeResult").textContent = "Invalid input";
}
}
</script>
</body>
</html>
211421205125

OUTPUT:

RESULT:
Thus the JavaScript program to calculate the Square and Cube of a number was
executed and the output has been verified successfully.

You might also like