Programming with JavaScript

⌘K
  1. Home
  2. Docs
  3. Programming with JavaScri...
  4. Example Programs
  5. Add and Subtract Numbers

Add and Subtract Numbers

Example 36

Write functions to add and subtract two numbers taken through html forms

<html>
    <head>
        <title> Form input </title>
    </head>
    <body>
        <form name="forma">
           Number 1 :   <input type="text" name="tb1" id="tb1"> <br> <br>
           Number 2 :   <input type="text" name="tb2" id="tb2"> <br> <br>
           <input type="button" value = "Addition" onclick="Add(document.forma.tb1.value, document.forma.tb2.value)"> 
           <input type="button" value = "Subtraction" onclick="Sub(document.forma.tb1.value, document.forma.tb2.value)"> <br>
           Result : <input type = "text" name = "tb3"> <br>
        </form>
        <script>
            function Add(n1,n2)
            {
                n1 = parseFloat(n1);
                n2 = parseFloat(n2);
                document.forma.tb3.value = n1 + n2;
            }
            function Sub(n1,n2)
            {
                n1 = parseFloat(n1);
                n2 = parseFloat(n2);
                document.forma.tb3.value = n1 - n2
            }
        </script>
    </body>
</html>

Output:

Loading

Views: 98

How can we help?

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments