-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4_basiccalculator.html
More file actions
73 lines (69 loc) · 1.73 KB
/
Copy path4_basiccalculator.html
File metadata and controls
73 lines (69 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html>
<html>
<head>
<title>Basic Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
padding-top: 50px;
}
input {
width: 100px;
padding: 8px;
margin: 5px;
font-size: 16px;
}
select, button {
padding: 8px;
font-size: 16px;
margin: 5px;
}
#result {
margin-top: 20px;
font-size: 20px;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Basic Calculator</h1>
<input type="number" id="num1" placeholder="Number 1">
<input type="number" id="num2" placeholder="Number 2">
<select id="operator">
<option value="+">+</option>
<option value="-">−</option>
<option value="*">×</option>
<option value="/">÷</option>
</select>
<button onclick="calculate()">Calculate</button>
<div id="result">Result: </div>
<script>
function calculate() {
const n1 = parseFloat(document.getElementById('num1').value);
const n2 = parseFloat(document.getElementById('num2').value);
const op = document.getElementById('operator').value;
let res;
if (isNaN(n1) || isNaN(n2)) {
res = "Please enter valid numbers.";
} else {
switch (op) {
case '+':
res = n1 + n2;
break;
case '-':
res = n1 - n2;
break;
case '*':
res = n1 * n2;
break;
case '/':
res = n2 !== 0 ? (n1 / n2).toFixed(2) : "Cannot divide by zero";
break;
}
}
document.getElementById('result').textContent = `Result: ${res}`;
}
</script>
</body>
</html>