6 PRGM
6 PRGM
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>
<metacharset="UTF-8">
<title>SimpleCalculator</title>
</head>
<body>
<divclass="calculator">
<h1>Simple Calculator</h1>
<div>
</div>
<div>
</div>
<div>
<button onclick="calculate('sum')">Sum</button>
<button onclick="calculate('difference')">Difference</button>
<button onclick="calculate('product')">Product</button>
<button onclick="calculate('quotient')">Quotient</button>
<button onclick="calculate('remainder')">Remainder</button>
<button onclick="calculate('power')">Power</button>
<button onclick="calculate('square')">Square</button>
</div>
</div>
<script src="script1.js"></script>
</body>
</html>
CSS Code:
body {
font-family:Arial, sans-serif;
background-color:#f0f0f0;
display:flex;
justify-content:center;
align-items:center;
height:100vh;
margin:0;
.calculator {
background-color:#fff;
padding:20px;
border-radius:10px;
text-align:center;
}
.calculator h1 {
margin-bottom:20px;
input[type="number"] {
width:200px;
padding:10px;
margin:10px 0;
font-size:16px;
button {
padding:10px 15px;
margin:5px;
font-size:16px;
cursor:pointer;
border:none;
background-color:#007bff;
color:white;
border-radius:5px;
button:hover {
background-color:#0056b3;
}
#result {
font-size:24px;
font-weight:bold;
function calculate(operation){
let result = 0;
switch (operation) {
case 'sum':
break;
case 'difference':
break;
case 'product':
break;
case 'quotient':
break;
case 'remainder':
case 'power':
break;
case 'square':
break;
case 'sqrt':
result = Math.sqrt(num1);
break;
default:
result = 0;
document.getElementById('result').innerText = result;
OUTPUT
Explanation of Code:
1. HTML:
o A simple form with two input fields for the user to enter numbers.
o Buttons are created for different operations (sum, difference, product,
quotient, remainder, power, square, square-root).
o Results are displayed inside a <span> element with the ID result.
2. CSS:
o Styling is applied to the calculator to make it visually appealing. The layout
is centered using Flexbox.
o Buttons are styled to change colors when hovered.
3. JavaScript:
o The calculate() function handles the mathematical operations based on
the button clicked.
o It retrieves the values from the input fields, performs the selected operation,
and displays the result.