Skip to content
Related Articles
Open in App
Not now

Related Articles

Find quotient and remainder by dividing an integer in JavaScript

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

In this article, we will find the quotient and remainder by dividing an integer in JavaScript. There are various methods to divide an integer number by another number and get its quotient and remainder. 

Example 1: This example uses the Math.floor() function to calculate the divisor. 

HTML




<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>
        Find quotient and remainder by dividing an integer in JavaScript
    </h3>
    <p id="Geek_up"></p>
    <button onclick="Geeks()">
        Click Here
    </button>
    <p id="Geek_down" style="color:green;"></p>
  
    <script>
        var up = document.getElementById("Geek_up");
        var a = 39;
        var b = 5;
        up.innerHTML = a + "/" + b;
        function Geeks() {
            var down = document.getElementById("Geek_down");
            down.innerHTML = "quotient = " + Math.floor(a/b)
                + "<br>" + "remainder = "+ a%b;
        }
    </script>
</body>

Output:

 

Example 2: This example uses the binary ~~ operator to calculate the divisor. 

HTML




<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>
        Integer division with remainder.
    </h3>
    <p id="Geek_up"></p>
    <button onclick="Geeks()">
        Click Here
    </button>
    <p id="Geek_down" style="color:green;"></p>
  
    <script>
        var up = document.getElementById("Geek_up");
        var a = 39;
        var b = 5;
        up.innerHTML = a + "/" + b;
        function Geeks() {
            var down = document.getElementById("Geek_down");
            var num = ~~(a / b);
            down.innerHTML = "quotient = " + num +
                "<br>" + "remainder = "+ a%b;
        }
    </script>
</body>

Output:

 

Example 3:This example uses the right shift >> operator to calculate the divisor. 

HTML




<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <h3>
        Integer division with remainder.
    </h3>
    <p id="Geek_up"></p>
    <button onclick="Geeks()">
        Click Here
    </button>
    <p id="Geek_down" style="color:green;"></p>
  
    <script>
        var up = document.getElementById("Geek_up");
        var a = 39;
        var b = 5;
        up.innerHTML = a + "/" + b;
        function Geeks() {
            var down = document.getElementById("Geek_down");
            var num = (a / b) >> 0;
            down.innerHTML = "quotient = " + num +
                "<br>" + "remainder = "+ a%b;
        }
    </script>
</body>

Output:

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!