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

Squareand Cube

The document describes a JavaScript program that takes a user-input number and calculates its square and cube. It outlines the algorithm, provides the program code, and verifies that the output was successful.

Uploaded by

Padmanaban M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Squareand Cube

The document describes a JavaScript program that takes a user-input number and calculates its square and cube. It outlines the algorithm, provides the program code, and verifies that the output was successful.

Uploaded by

Padmanaban M
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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