Calculator Source Code
Calculator Source Code
DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<style>
body {
background-color: #f0f0f0;
font-family: Arial, sans-serif;
}
.calculator {
background: #fff;
padding: 20px;
width: 260px;
margin: 50px auto;
border-radius: 10px;
box-shadow: 0 0 15px rgba(0,0,0,0.2);
}
.calculator h2 {
text-align: center;
margin-bottom: 20px;
}
#textbox {
width: 100%;
height: 40px;
font-size: 20px;
text-align: right;
padding-right: 10px;
border: 2px solid #ccc;
border-radius: 5px;
box-sizing: border-box;
}
table {
width: 100%;
margin-top: 20px;
}
th {
width: 25%;
padding: 15px 0;
font-size: 18px;
background-color: #e0e0e0;
cursor: pointer;
border-radius: 5px;
transition: background 0.2s;
}
th:hover {
background-color: #ccc;
}
.equal_op {
background-color: #4CAF50;
color: white;
height: 100%;
}
.equal_op:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="calculator">
<h2>Calculator</h2>
<input type="text" id="textbox" readonly>
<table>
<tr>
<th onclick="clr()">C</th>
<th onclick="add('/')">/</th>
<th onclick="add('*')">*</th>
<th onclick="add('-')">-</th>
</tr>
<tr>
<th onclick="add('7')">7</th>
<th onclick="add('8')">8</th>
<th onclick="add('9')">9</th>
<th onclick="add('+')">+</th>
</tr>
<tr>
<th onclick="add('4')">4</th>
<th onclick="add('5')">5</th>
<th onclick="add('6')">6</th>
<th onclick="add('%')">%</th>
</tr>
<tr>
<th onclick="add('1')">1</th>
<th onclick="add('2')">2</th>
<th onclick="add('3')">3</th>
<th class="equal_op" rowspan="2" onclick="equate()">=</th>
</tr>
<tr>
<th colspan="2" onclick="add('0')">0</th>
<th onclick="add('.')">.</th>
</tr>
</table>
</div>
<script>
function add(character) {
document.getElementById("textbox").value += character;
}
function clr() {
document.getElementById("textbox").value = "";
}
function equate() {
try {
let result = eval(document.getElementById("textbox").value);
document.getElementById("textbox").value = result;
} catch (e) {
document.getElementById("textbox").value = "Error";
}
}
</script>
</body>
</html>