Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.8 kB
2
Indexable
Never
<!DOCTYPE html>
<html>
<head>
<title>Simple Calculator</title>
<script>
// Function to perform the addition operation
function add() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
var result = num1 + num2;
document.getElementById("result").value = result;
}
// Function to perform the subtraction operation
function subtract() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
var result = num1 - num2;
document.getElementById("result").value = result;
}
DEPARTMENT OF ISE, RIT,
BANGALORE
23
// Function to perform the multiplication operation
function multiply() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
var result = num1 * num2;
document.getElementById("result").value = result;
}
// Function to perform the division operation
function divide() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
var result = num1 / num2;
if (isNaN(result)) {
document.getElementById("result").value = "Cannot divide by zero";
} else {
document.getElementById("result").value = result;
}
}
</script>
</head>
<body>
<h1>Simple Calculator</h1>
<label for="num1">Number 1:</label>
<input type="number" id="num1"><br>
<label for="num2">Number 2:</label>
<input type="number" id="num2"><br>
<button onclick="add()">Add</button>
<button onclick="subtract()">Subtract</button>
<button onclick="multiply()">Multiply</button>
<button onclick="divide()">Divide</button>
<label for="result">Result:</label>
<input type="text" id="result" readonly>
</body>
</html>
Leave a Comment