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

211421205125

ARITHMETIC OPERATIONS

AIM:
To write a JavaScript program that performs arithmetic operations with the given numbers
and provides the result.

ALGORITHM:
Step 1: Start the process.

Step 2: Read and store the first number.

Step 3: Read and store the second number.

Step 4: Read and store the desired arithmetic operation.

Step 5: Perform the selected operation.

Step 6: Display the result.

Step 7: Stop the process.

PROGRAM:
<!DOCTYPE html>
<html>
<head>
<title>Arithmetic Operations</title>
</head>
<body>
<h1>Arithmetic Operations</h1>
<form id="calculator">
<label for="num1">Enter Number 1:</label>
<input type="number" id="num1" name="num1" required><br><br>
<label for="num2">Enter Number 2:</label>
<input type="number" id="num2" name="num2" required><br><br>
<label for="operation">Select Operation:</label>
<select id="operation" name="operation">
<option value="add">Addition</option>
<option value="subtract">Subtraction</option>
<option value="multiply">Multiplication</option>
211421205125

<option value="divide">Division</option>
</select><br><br>
<input type="button" value="Calculate" onclick="calculate()">
</form>
<h2>Result:</h2>
<p id="result"></p>
<script> function
calculate() {
const num1 = parseFloat(document.getElementById('num1').value);
const num2 =
parseFloat(document.getElementById('num2').value); const
operation = document.getElementById('operation').value;
let result;
switch (operation) {
case 'add':
result = num1 + num2;
break;
case 'subtract':
result = num1 - num2;
break;
case 'multiply':
result = num1 * num2;
break;
case 'divide':
if (num2 === 0) {
result = "Division by zero is not allowed.";
}
else {
result = num1/num2;
}
break;
default:
result = "Invalid operation";
}
211421205125

document.getElementById('result').textContent = `Result: $
{result}`;
}
</script>
</body>
</html>

OUTPUT:

RESULT:
Thus the JavaScript program to perform arithmetic operations with the given two numbers
was executed and the output has been verified successfully.

You might also like