Tuesday, August 8, 2023

Write how to make a calculator using java program

 

    Write how to make a calculator using java program




here are the steps on how to make a calculator using HTML:

  1. Create the basic structure of the calculator. This includes the following elements:

    • A result box where the calculation results will be displayed.
    • Buttons for performing mathematical operations.
    • Text fields for entering numbers.
  2. Create a CSS style sheet to style the calculator. This includes the following styles:

    • The result box should be centered and have a border.
    • The buttons should be large and have a white background and black text.
    • The text fields should be small and have a black border.
  3. Create the JavaScript code to calculate the inputs and display the results. This code includes the following functions:

    • clearScreen(): Clears the result box.
    • display(): Displays a value in the result box.
    • calculate(): Performs a mathematical operation on the values in the text fields and displays the result in the result box.

Here is an example of the HTML, CSS, and JavaScript code for a simple calculator:

HTML
<!DOCTYPE html>
<html>
<head>
<title>Calculator</title>
<style>
body {
font-family: sans-serif;
}

.calculator {
width: 300px;
margin: 0 auto;
}

.result-box {
width: 100%;
height: 50px;
border: 1px solid black;
text-align: center;
}

.button {
width: 50px;
height: 50px;
background-color: white;
color: black;
border: none;
cursor: pointer;
}

.text-field {
width: 100px;
height: 25px;
border: 1px solid black;
}
</style>
</head>
<body>
<div class="calculator">
<div class="result-box">
<input type="text" id="result" value="0" readonly />
</div>
<div class="buttons">
<button id="clear">Clear</button>
<button id="plus">+</button>
<button id="minus">-</button>
<button id="multiply">*</button>
<button id="divide">/</button>
<input type="text" id="first-number" />
<input type="text" id="second-number" />
</div>
</div>
<script>
function clearScreen() {
document.getElementById("result").value = "";
}

function display(value) {
document.getElementById("result").value += value;
}

function calculate() {
var firstNumber = document.getElementById("first-number").value;
var secondNumber = document.getElementById("second-number").value;
var operator = document.getElementById("operator").value;

var result = eval(firstNumber + operator + secondNumber);

document.getElementById("result").value = result;
}

document.getElementById("clear").onclick = clearScreen;
document.getElementById("plus").onclick = function() {
display("+");
};
document.getElementById("minus").onclick = function() {
display("-");
};
document.getElementById("multiply").onclick = function() {
display("*");
};
document.getElementById("divide").onclick = function() {
display("/");
};
document.getElementById("calculate").onclick = calculate;
</script>
</body>
</html>

This is just a basic example of how to make a calculator using HTML, CSS, and JavaScript. You can customize the code to add more features and functionality to your calculator.

No comments:

Post a Comment

Python

    Python Int roduction Python is a widely-used, high-level, interpreted programming language created by Guido van Rossum in 1991. It...