Find quotient and remainder by dividing an integer in JavaScript - GeeksforGeeks

23 Apr, 2019
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.
<!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>                    
Output:
Before clicking on the button:
After clicking on the button:
Example 2: This example uses the binary ~~ operator to calculate the divisor.
<!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>                    
Output:
Before clicking on the button:
After clicking on the button:
Example 3:This example uses the right shift >> operator to calculate the divisor.
<!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>                    
Output:
Before clicking on the button:
After clicking on the button:
javascript-math
JavaScript-Misc
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
How to calculate the number of days between two dates in javascript?
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Convert a string to an integer in JavaScript