<!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> <script> function add() { const val1 = parseFloat(document.getElementById('value1').value); const val2 = parseFloat(document.getElementById('value2').value); const result = val1 + val2; document.getElementById('result').textContent = `Addition of ${val1} and ${val2} is ${result}`; } function sub() { const val1 = parseFloat(document.getElementById('value1').value); const val2 = parseFloat(document.getElementById('value2').value); const result = val1 - val2; document.getElementById('result').textContent = `Subtraction of ${val1} and ${val2} is ${result}`; } function mul() { const val1 = parseFloat(document.getElementById('value1').value); const val2 = parseFloat(document.getElementById('value2').value); const result = val1 * val2; document.getElementById('result').textContent = `Multiplication of ${val1} and ${val2} is ${result}`; } function div() { const val1 = parseFloat(document.getElementById('value1').value); const val2 = parseFloat(document.getElementById('value2').value); if (val2 === 0) { document.getElementById('result').textContent = "Error: Division by zero"; } else { const result = val1 / val2; document.getElementById('result').textContent = `Division of ${val1} and ${val2} is ${result}`; } } </script> </head> <body> <h1>Simple Calculator</h1> <h3>Simple Calculator</h3> <label for="value1">Value 1:</label> <input type="number" id="value1" placeholder="Enter first number"> <br> <label for="value2">Value 2:</label> <input type="number" id="value2" placeholder="Enter second number"> <br><br> <button name="add" onclick="add()">ADDITION</button> <button name="sub" onclick="sub()">SUBTRACT</button> <button name="mul" onclick="mul()">MULTIPLY</button> <button name="div" onclick="div()">DIVISION</button> <br><br> <div id="result">Result: </div> </body> </html>
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter