Write a program for Making a basic calculator, which can apply on two operands.

1
2
    <html>
3
    <head>
4
        <script>
5
6
            function add() {
7
                var a = document.getElementById("a1").value;
8
                var b = document.getElementById("a2").value;
9
                var c = +(a + b);
10
                document.getElementById("out").innerHTML = "Addition is " + c;
11
            }
12
            function sub() {
13
                var a = document.getElementById("a1").value;
14
                var b = document.getElementById("a2").value;
15
                var c = a - b;
16
                document.getElementById("out").innerHTML = "Subtraction is " + c;
17
            } function mul() {
18
                var a = document.getElementById("a1").value;
19
                var b = document.getElementById("a2").value;
20
                var c = a * b;
21
                document.getElementById("out").innerHTML = "Multipliction is" + c;
22
            } function divi() {
23
                var a = document.getElementById("a1").value;
24
                var b = document.getElementById("a2").value;
25
                var c = a / b;
26
                document.getElementById("out").innerHTML = "Division is" + c;
27
            }
28
        </script>
29
    </head>
30
    <body>
31
        <div id="inp">
32
            <center>
33
                <h1>INPUTS </h1> <br><br>
34
                Enter the 1st number <input type=value id="a1">
35
                <br><br>
36
                Enter the 2nd number <input type=value id="a2">
37
            </center>
38
        </div>
39
        <hr>
40
        <div id="oper">
41
            <center>
42
                <button type="button" onclick="add()">
43
                    <pre> + </pre>
44
                </button>
45
                <button type="button" onclick="sub()">
46
                    <pre> - </pre>
47
                </button>
48
                <button type="button" onclick="mul()">
49
                    <pre> * </pre>
50
                </button>
51
                <button type="button" onclick="divi()">
52
                    <pre> / </pre>
53
                </button>
54
            </center>
55
        </div>
56
        <hr>
57
        <div id="output">
58
            <center>
59
                <h1> OUTPUT </h1>
60
61
                <br><br><br>
62
63
                <h1 id="out">
64
65
                </h1>
66
            </center>
67
        </div>
68
    </body>
69
    </html>
70
71
72
    

Output

1
2
        No Output Available
3
        

Rate the Code

Reviews are published after a quick safety check (usually within 24 hours).

Reviews are public. Developers can see basic device specifications to respond. Learn more

Core Concept of a basic calculator, which can apply to two operands

With the help of input elements and user-defined JS methods, the basic arithmetic operations are applied, and the result is shown in an element that contains the output ID.

Why is it used?

To make a dynamic web page, users can interact or enter some data, and the web page will give the result without reloading the page.

Real-life Applications

  • Simple calculator/utility Apps: Like online financial calculators, currency converters, or basic math widgets.
  • E-Commerce Cart Calculations: To calculate the product total and bill based on the quantity in a cart.

Benefits of this Program

  • Instant client-side processing: All calculators perform on the client side based on the existing data, giving an instant result.
License: This program's source code is open-sourced under the MIT License, while the rest of the program's text content and overall dhimanp platform and brand content remain proprietary under dhimanp.

Submit your modified code

Your submissions are published after a quick safety check (usually within 7 days).

Submissions are public. Developers can see basic device specifications to respond. Learn more

Disclaimer

The information that you shared with us will be publicly visible, and everyone can see your information. And we (dhimanp) are not responsible if anyone misuses this information. We can remove any submitted code at any time for better transparency.

Privacy Policy

If you share your data with us by submitting your code for credit, then we may show your full name, school or college name, and city name on our website.

We are not using this data to track you and are not selling this data to anyone.

If you want to remove your name from our website, you can contact us, and we will remove your name from our website. In the removal process, full credit, including your full name, school or college, city, and code, will be removed from our website.