Find 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
  
<head
    <title
        Integer division with remainder. 
    </title
</head
  
<body style = "text-align:center;"
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1
      
    <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 = "divisor = " + Math.floor(a/b)
                + "<br>" + "remainder = "+ a%b;
        }
    </script
</body
  
</html>                    

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
  
<head
    <title
        Integer division with remainder. 
    </title
</head
  
<body style = "text-align:center;"
      
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1
      
    <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 = "divisor = " + num + 
                "<br>" + "remainder = "+ a%b;
        
    </script
</body
  
</html>                    

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html> 
<html
  
<head
    <title
        Integer division with remainder. 
    </title
</head
  
<body style = "text-align:center;"
    <h1 style = "color:green;"
        GeeksForGeeks 
    </h1
      
    <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 = "divisor = " + num + 
                "<br>" + "remainder = "+ a%b;
        
    </script
</body
  
</html>                    

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.