0% found this document useful (0 votes)
3 views

WT4

The document contains code for a simple calculator implemented using HTML, CSS, and JavaScript. It features buttons for numbers, basic operations, and a square function, along with styling for a user-friendly interface. The JavaScript handles input, operations, and calculations, ensuring proper user interaction and error handling.

Uploaded by

pratikanarse577
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

WT4

The document contains code for a simple calculator implemented using HTML, CSS, and JavaScript. It features buttons for numbers, basic operations, and a square function, along with styling for a user-friendly interface. The JavaScript handles input, operations, and calculations, ensuring proper user interaction and error handling.

Uploaded by

pratikanarse577
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical No – 4

Code
Html-
<!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>

<link rel="stylesheet" href="styles.css">

</head>

<body>

<div class="calculator">

<input type="text" id="input" placeholder="Enter number" />

<div class="buttons">

<button onclick="appendNumber(1)">1</button>

<button onclick="appendNumber(2)">2</button>

<button onclick="appendNumber(3)">3</button>

<button onclick="setOperation('+')">+</button>

<button onclick="appendNumber(4)">4</button>

<button onclick="appendNumber(5)">5</button>

<button onclick="appendNumber(6)">6</button>

<button onclick="setOperation('-')">-</button>

<button onclick="appendNumber(7)">7</button>

<button onclick="appendNumber(8)">8</button>

<button onclick="appendNumber(9)">9</button>

<button onclick="setOperation('*')">*</button>

<button onclick="appendNumber(0)">0</button>

<button onclick="calculateSquare()">x²</button>

<button onclick="calculateResult()">=</button>
<button onclick="setOperation('/')">/</button>

<button onclick="clearInput()">C</button>

</div>

</div>

<script src="script.js"></script>

</body>

</html>

CSS-
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f4f4f4;
}

.calculator {
border: 1px solid #ccc;
padding: 20px;
border-radius: 10px;
background-color: white;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

input {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}

button {

padding: 15px;
font-size: 18px;
border: none;
border-radius: 5px;
cursor: pointer;
background-color: #007bff;
color: white;
}

button:hover {
background-color: #0056b3;
}

JavaScript
let currentInput = &#39;&#39;;
let operation = &#39;&#39;;

function appendNumber(num) {
currentInput += num.toString();
document.getElementById(&#39;input&#39;).value = currentInput;
}

function setOperation(op) {
if (isNaN(currentInput) || currentInput === &#39;&#39;) {
alert(&#39;Please enter a valid number first.&#39;);
return;
}
operation = op;
currentInput += ` ${op} `;
document.getElementById(&#39;input&#39;).value = currentInput;
}

function calculateResult() {
try {

let result = eval(currentInput.replace(&#39; &#39;, &#39;&#39;));


if (isNaN(result)) {
alert(&#39;Invalid calculation.&#39;);
return;
}
document.getElementById(&#39;input&#39;).value = result;
currentInput = result.toString();
} catch (error) {
alert(&#39;Error in calculation&#39;);
}
}

function calculateSquare() {
if (isNaN(currentInput) || currentInput === &#39;&#39;) {
alert(&#39;Please enter a valid number first.&#39;);
return;
}
let number = parseFloat(currentInput);
let result = number * number;
document.getElementById(&#39;input&#39;).value = result;
currentInput = result.toString();
}
function clearInput() {
currentInput = &#39;&#39;;
operation = &#39;&#39;;
document.getElementById(&#39;input&#39;).value = &#39;&#39;;
}

OUPUT

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy