Experiment 06
Experiment 06
Experiment-06
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.
Program:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Calculator</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
Search Creators…. Page 52
BCSL504 | Web Technology Lab|
background-color: #f0f0f0;
.calculator {
background-color: #fff;
border-radius: 8px;
padding: 20px;
width: 300px;
#display {
width: 100%;
height: 40px;
font-size: 1.5em;
text-align: right;
margin-bottom: 10px;
padding: 5px;
box-sizing: border-box;
.buttons {
display: grid;
gap: 10px;
button {
padding: 10px;
font-size: 1.2em;
border: none;
background-color: #e0e0e0;
cursor: pointer;
border-radius: 4px;
button:hover {
background-color: #d0d0d0;
.operator {
background-color: #f0a030;
color: white;
.operator:hover {
background-color: #e09020;
</style>
</head>
<body>
<div class="calculator">
<div class="buttons">
<button onclick="appendToDisplay('7')">7</button>
<button onclick="appendToDisplay('8')">8</button>
<button onclick="appendToDisplay('9')">9</button>
<button class="operator"
onclick="setOperation('+')">+</button>
<button onclick="appendToDisplay('4')">4</button>
<button onclick="appendToDisplay('5')">5</button>
<button onclick="appendToDisplay('6')">6</button>
<button onclick="appendToDisplay('1')">1</button>
Search Creators…. Page 55
BCSL504 | Web Technology Lab|
<button onclick="appendToDisplay('2')">2</button>
<button onclick="appendToDisplay('3')">3</button>
<button class="operator"
onclick="setOperation('*')">×</button>
<button onclick="appendToDisplay('0')">0</button>
<button onclick="appendToDisplay('.')">.</button>
<button class="operator"
onclick="setOperation('/')">÷</button>
<button class="operator"
onclick="setOperation('^')">x<sup>y</sup></button>
<button class="operator"
onclick="square()">x<sup>2</sup></button>
<button onclick="clearDisplay()">C</button>
</div>
</div>
<script>
function appendToDisplay(value) {
currentValue += value;
display.value = currentValue;
function clearDisplay() {
currentValue = '';
operation = '';
previousValue = '';
display.value = '';
function setOperation(op) {
calculate();
operation = op;
previousValue = currentValue;
currentValue = '';
function calculate() {
let result;
switch(operation) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
case '%':
break;
case '^':
break;
display.value = result;
previousValue = result.toString();
currentValue = '';
operation = '';
function squareRoot() {
display.value = result;
currentValue = result.toString();
function square() {
display.value = result;
currentValue = result.toString();
</script>
</body>
</html>
Explanation
The <head> section contains metadata, title, and embedded CSS styles.
The calculator has a white background with rounded corners and a shadow.
Buttons are created for numbers 0-9, decimal point, operators (+, -, *, /), equals,
clear, and additional functions (%, ^, √, x²).
The equals button triggers the calculate() function, which performs the operation
and displays the result.
The calculator includes modulo (%), exponentiation (^), square root (√), and
square (x²) operations.
Key Features: