Quick calculator using HTML,CSS and Java Script.
>>HTML codes.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="cal.css">
<title>Calculator</title>
</head>
<body>
<div class="calculator">
<input type="text" id="display" disabled>
<div class="buttons">
<button onclick="appendToDisplay('7')">7</button>
<button onclick="appendToDisplay('8')">8</button>
<button onclick="appendToDisplay('9')">9</button>
<button onclick="appendToDisplay('+')">+</button>
<button onclick="appendToDisplay('4')">4</button>
<button onclick="appendToDisplay('5')">5</button>
<button onclick="appendToDisplay('6')">6</button>
<button onclick="appendToDisplay('-')">-</button>
<button onclick="appendToDisplay('1')">1</button>
<button onclick="appendToDisplay('2')">2</button>
<button onclick="appendToDisplay('3')">3</button>
<button onclick="appendToDisplay('*')">*</button>
<button onclick="appendToDisplay('0')">0</button>
<button onclick="clearDisplay()">C</button>
<button onclick="calculate()">=</button>
<button onclick="appendToDisplay('/')">/</button>
</div>
</div>
</container>
</body>
<script src="cal.js"></script>
</html>
>>CSS codes.
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color:black;
}
.calculator {
width: 300px;
border: 1px solid #ccc;
border-radius: 5px;
padding: 10px;
background-color: #fff;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.1);
}
#display {
width: 100%;
height: 50px;
font-size: 20px;
text-align: right;
margin-bottom: 10px;
border: 1px solid:whitesmoke;
border-radius: 5px;
padding: 5px;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 5px;
}
button {
width: 95%;
height: 50px;
font-size: 25px;
background-color: red;
border: none;
border-radius: 5px;
cursor: pointer;
transition: background-color 0.2s;
}
button:hover {
background-color: #ccc;
}
>> JAVA codes
let displayValue = "";
function appendToDisplay(value) {
displayValue += value;
document.getElementById("display").value = displayValue;
}
function clearDisplay() {
displayValue = "";
document.getElementById("display").value = displayValue;
}
function calculate() {
try {
displayValue = eval(displayValue).toString();
document.getElementById("display").value = displayValue;
} catch (error) {
document.getElementById("display").value = "Error";
}
}
>> Output data:
Comments
Post a Comment