Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

We will see the calculation such as multiplication and division using Javascript. An arithmetic operation operates on two numbers and numbers are called operands. 

Approach: Multiplication of two numbers

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.

The multiplication operator (*) multiplies two or more numbers and returns the product of the operands.

Syntax:

a*b

Example: In this example, we will use the multiplication operator to multiply two numbers.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
 
<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>
 
</html>


Output:

Approach: Division of two numbers

Division The division operator (/) divides two or more numbers. the divided operand is known as the “dividend,” while the dividing operand is referred to as the “divisor.” The “quotient” is the value that remains after division.

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.

Syntax:

a / b

Example:

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

Example: Below is the implementation of the above approach:

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
 
<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>
 
</html>


Output:

multiplication and division of two numbers using JavaScript

multiplication and division of two numbers using JavaScript



Last Updated : 10 Jan, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads