Skip to content
Related Articles
Open in App
Not now

Related Articles

How to calculate multiplication and division of two numbers using JavaScript ?

Improve Article
Save Article
  • Last Updated : 06 Jan, 2023
Improve Article
Save Article

In this article, we will see the calculation such as multiplication and division using Javascript. An arithmetic operation operates on two numbers and numbers are called operands. 

Multiplication The multiplication operator (*) multiplies two or more numbers.

Example:

var a =1 5;
var b = 12;
var c = a × b;

Approach: Create the html form to take input from the user to perform multiplication operations. Add javascript code inside html to perform multiplication logic. Document.getElementById(id).value property returns the value of the value attribute of a text field.

Example: Below is the implementation of the above approach:

HTML




<body style="margin: 30px">
    <h1 style="color:green">GeeksforGeeks</h1>
    <h3>Multiplication using Javascript</h3>
    <form>
        1st Number : <input type="text" id="firstNumber" /><br>
        2nd Number: <input type="text" id="secondNumber" /><br>
        <input type="button" onClick="multiplyBy()" Value="Multiply" /><br>
    </form>
 
    <p>The Result is : <br>
        <span id="result"></span>
    </p>
 
 
    <script>
        function multiplyBy()
        {
          num1 = document.getElementById(
            "firstNumber").value;
          num2 = document.getElementById(
            "secondNumber").value;
          document.getElementById(
            "result").innerHTML = num1 * num2;
        }
    </script>
</body>


Output:

multiplication and division of two numbers using JavaScript

multiplication and division of two numbers using JavaScript

Division The division operator (/) divides two or more numbers.

Example:

var a = 50;
var b = 20;
var c = a / b;

Approach: Create the HTML form to take input from the user to perform division operations. Add JavaScript code inside HTML to perform division logic. Document.getElementById(id).value property returns the value of the value attribute of a text field.

Example: Below is the implementation of the above approach:

HTML




<body style="margin: 30px">
    <h1 style="color:green">GeeksforGeeks</h1>
    <h3>Division using Javascript</h3>
    <form>
        1st Number : <input type="text" id="firstNumber" /><br>
        2nd Number: <input type="text" id="secondNumber" /><br>
        <input type="button" onClick="divideBy()" Value="Divide" />
    </form>
 
    <p>The Result is : <br>
        <span id="result"></span>
    </p>
 
 
    <script>
        function divideBy()
        {
          num1 = document.getElementById(
            "firstNumber").value;
          num2 = document.getElementById(
            "secondNumber").value;
          document.getElementById(
            "result").innerHTML = num1 / num2;
        }
    </script>
</body>


Output:

multiplication and division of two numbers using JavaScript

multiplication and division of two numbers using JavaScript


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!