6. Apply HTML, CSS and JavaScript to design a simple calculator to perform the following operations: sum, product, difference, remainder, quotient, power, square-root and square.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple Calculator</title>
<style>
/* Body Styling */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #dfe1e3;
color: #ecf0f1;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}
/* Calculator Container */
.calculator {
background-color: #34495e;
padding: 20px;
border-radius: 15px;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.5);
width: 320px;
}
/* Display Styling */
#result {
width: 94%;
height: 60px;
font-size: 1.5em;
padding: 10px;
margin-bottom: 15px;
border: none;
border-radius: 10px;
background-color: #f763e1;
color: #2c3e50;
text-align: right;
}
/* Button Styling */
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
input[type="button"] {
width: 100%;
height: 60px;
font-size: 1.2em;
border: none;
border-radius: 10px;
background-color: #3498db;
color: #ecf0f1;
cursor: pointer;
transition: all 0.3s ease;
}
input[type="button"]:hover {
background-color: #2980b9;
}
input[type="button"]:active {
background-color: #1c6ea4;
}
/* Special Buttons */
.equal {
background-color: #e74c3c;
}
.equal:hover {
background-color: #c0392b;
}
.clear {
background-color: #f39c12;
}
.clear:hover {
background-color: #e67e22;
}
</style>
</head>
<body>
<div class="calculator">
<h1>Simple Calculator</h1>
<input type="text" id="result" disabled>
<div class="buttons">
<input type="button" value="1" onclick="append('1')">
<input type="button" value="2" onclick="append('2')">
<input type="button" value="3" onclick="append('3')">
<input type="button" value="+" onclick="append('+')">
<input type="button" value="4" onclick="append('4')">
<input type="button" value="5" onclick="append('5')">
<input type="button" value="6" onclick="append('6')">
<input type="button" value="-" onclick="append('-')">
<input type="button" value="7" onclick="append('7')">
<input type="button" value="8" onclick="append('8')">
<input type="button" value="9" onclick="append('9')">
<input type="button" value="*" onclick="append('*')">
<input type="button" value="0" onclick="append('0')">
<input type="button" value="C" class="clear" onclick="clearResult()">
<input type="button" value="=" class="equal" onclick="calculate()">
<input type="button" value="/" onclick="append('/')">
</div>
</div>
<script>
// Function to append value to the input field
function append(value) {
document.getElementById('result').value += value;
}
// Function to clear the input field
function clearResult() {
document.getElementById('result').value = '';
}
// Function to calculate the result
function calculate() {
try {
let result = eval(document.getElementById('result').value);
document.getElementById('result').value = result;
} catch (error) {
alert('Invalid Expression');
clearResult();
}
}
</script>
</body>
</html>