<html>
<head>
<title>Calculator</title>
</head>
<body>
<h1>Calculator</h1>
<label for=”num1″>Enter first number:</label>
<input type=”number” id=”num1″><br><br>
<label for=”operation”>Select operation:</label>
<select id=”operation”>
<option value=”+”>Add (+)</option>
<option value=”-“>Sub (-)</option>
<option value=”*”>Multiply (*)</option>
<option value=”/”>Divide (/)</option>
<option value=”%”>Reminder(%)</option>
</select><br><br>
<label for=”num2″>Enter second number:</label>
<input type=”number” id=”num2″><br><br>
<button onclick=”calculate()”>Calculate</button><br><br>
<p id=”result”>Result: </p>
<script>
function calculate() {
const num1 = parseInt(document.getElementById(“num1”).value);
const num2 = parseInt(document.getElementById(“num2”).value);
const operation = document.getElementById(“operation”).value;
let result;
switch (operation) {
case ‘+’:
result = num1 + num2;
break;
case ‘-‘:
result = num1 – num2;
break;
case ‘*’:
result = num1 * num2;
break;
case ‘/’:
if (num2 != 0) {
result = num1 / num2;
} else {
result = “Cannot divide by zero!”;
}
break;
case ‘%’:
result=num1 % num2;
break;
default:
result = “Invalid operation”;
}
document.getElementById(“result”).textContent = `Result: ${result}`;
}
</script>
</body>
</html>
Please follow and like us: