This code creates a simple calculator with four basic math operations: addition, subtraction, multiplication, and division. It consists of an HTML structure for the calculator layout, CSS for styling, and JavaScript for performing the calculations and updating the result dynamically.
To use the calculator, enter the two numbers in the input fields and click on the corresponding operation button. The result will be displayed below. Note that this is a basic example, and you can expand it to include more advanced functionality or additional operations as need.
The calculator is implemented using HTML, CSS, and JavaScript. The HTML structure defines the layout of the calculator, including input fields for the numbers and buttons for each operation. CSS styles are applied to enhance the visual appearance of the calculator, giving it a clean and user-friendly design.
The JavaScript code handles the logic behind the calculator's functionality. It includes four functions, one for each operation, which are triggered when the corresponding operation button is clicked. Inside each function, the user-inputted numbers are retrieved, the specified arithmetic operation is performed, and the result is displayed dynamically in the designated area of the calculator interface.
The math calculator provides a convenient way for users to quickly perform basic math calculations without the need for a physical calculator or manual computation. It can be used for a variety of purposes, such as solving simple math problems, verifying calculations, or performing quick calculations in day-to-day activities.
Overall, the math calculator serves as a handy tool for individuals who need to perform basic arithmetic operations accurately and efficiently, offering a user-friendly interface and immediate results.
Coding
✓
<html>
<head>
<title>Math Calculator</title>
<style>
.calculator {
max-width: 300px;
margin: 0 auto;
padding: 20px;
border: 1px solid #ccc;
border-radius: 5px;
}
input[type="number"],
button {
margin-bottom: 10px;
}
button {
width: 100%;
padding: 10px;
font-size: 16px;
background-color: #4CAF50;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
button:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="calculator">
<h2>Math Calculator</h2>
<input type="number" id="num1" placeholder="Enter number 1">
<input type="number" id="num2" placeholder="Enter number 2">
<button onclick="add()">Add</button>
<button onclick="subtract()">Subtract</button>
<button onclick="multiply()">Multiply</button>
<button onclick="divide()">Divide</button>
<h4>Result: <span id="result"></span></h4>
</div>
<script>
function add() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
var result = num1 + num2;
document.getElementById("result").textContent = result;
}
function subtract() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
var result = num1 - num2;
document.getElementById("result").textContent = result;
}
function multiply() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
var result = num1 * num2;
document.getElementById("result").textContent = result;
}
function divide() {
var num1 = parseFloat(document.getElementById("num1").value);
var num2 = parseFloat(document.getElementById("num2").value);
var result = num1 / num2;
document.getElementById("result").textContent = result;
}
</script>
</body>
</html>
No comments:
Post a Comment