Skip to content

write a JavaScript program to calculate multiplication and division of two numbers (input from a user)

  • by

You can do calculate the multiplication and division of two numbers (input from a user) by storing the user-entered value into the variables and then perform operations like Multiplication or division by the math formula.

JavaScript Calculate multiplication and division of two numbers example

HTML example code: A program you will learn about how to find Multiplication and Division of two numbers using JavaScript.

<!DOCTYPE html>
<html> 
<body>
    <form>
        1st Number : <input type="text" id="fn" /><br>
        2nd Number: <input type="text" id="sn" /><br>

        <input type="button" onClick="multiplyBy()" Value="Multiply" />
        <input type="button" onClick="divideBy()" Value="Divide" />
    </form>
    
    <p>The Result is : <br>
        <span id = "result"></span>
    </p>

    <script type="text/javascript">

     function multiplyBy()
     {
        num1 = document.getElementById("fn").value;
        num2 = document.getElementById("sn").value;
        document.getElementById("result").innerHTML = num1 * num2;
    }

    function divideBy() 
    { 
        num1 = document.getElementById("fn").value;
        num2 = document.getElementById("sn").value;
        document.getElementById("result").innerHTML = num1 / num2;
    } 
</script>
</body>
</html>

Output:

JavaScript Calculate multiplication and division of two numbers HTML example

Explanation:

document.getElementById(id).value: The value property sets or returns the value of the value attribute of a input text field.

document.getElementById(“result”).innerHTM : The innerHTML property sets or returns the HTML content (inner HTML) of an element.

Do comment if you have any doubts and suggestion on this topic examples codes.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *