<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Calculator</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f3f3f3;
}
.calculator {
width: 300px;
background: #fff;
border-radius: 10px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
padding: 20px;
}
.display {
width: 100%;
height: 50px;
background: #e6e6e6;
border: none;
border-radius: 5px;
font-size: 1.5rem;
text-align: right;
padding: 10px;
margin-bottom: 15px;
box-sizing: border-box;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
button {
height: 50px;
font-size: 1.2rem;
border: none;
border-radius: 5px;
cursor: pointer;
background: #007BFF;
color: white;
}
button:active {
background: #0056b3;
}
button.clear {
background: #f44336;
}
button.equal {
background: #28a745;
grid-column: span 2;
}
</style>
</head>
<body>
<div class="calculator">
<input type="text" id="display" class="display" disabled />
<div class="buttons">
<button onclick="appendValue('7')">7</button>
<button onclick="appendValue('8')">8</button>
<button onclick="appendValue('9')">9</button>
<button onclick="appendValue('/')">/</button>
<button onclick="appendValue('4')">4</button>
<button onclick="appendValue('5')">5</button>
<button onclick="appendValue('6')">6</button>
<button onclick="appendValue('*')">*</button>
<button onclick="appendValue('1')">1</button>
<button onclick="appendValue('2')">2</button>
<button onclick="appendValue('3')">3</button>
<button onclick="appendValue('-')">-</button>
<button onclick="appendValue('0')">0</button>
<button onclick="appendValue('.')">.</button>
<button onclick="clearDisplay()" class="clear">C</button>
<button onclick="appendValue('+')">+</button>
<button onclick="calculateResult()" class="equal">=</button>
</div>
</div>
<script>
const display = document.getElementById('display');
function appendValue(value) {
display.value += value;
}
function clearDisplay() {
display.value = '';
}
function calculateResult() {
try {
display.value = eval(display.value) || '';
} catch {
display.value = 'Error';
}
}
</script>
</body>
</html>