How to perform integer division and get the remainder in JavaScript?

JavascriptWeb DevelopmentFront End Technology

In this tutorial, we will learn how to perform integer division and get the remainder in JavaScript.

The division operator (/) returns the quotient of its operands, where the dividend is on the left, and the divisor is on the right. When another splits one operand, the remainder operator (%) returns the residual. It always takes the dividend sign.

In the operation x% y, x is known as the dividend, and y is known as the divisor. If one of the operands is NaN, x is Infinity, or y is 0, the operation returns NaN. Otherwise, the dividend x is returned if y is Infinity or x is 0.

Following are the methods to perform integer division and get the remainder in JavaScript.

Approach 1: Using the % Operator

When one operand is divided by another, the remainder (%) operator returns the remainder. The return value using this is always negative when the first operand is negative, and vice versa when a first operand is a positive number. The remainder operator will always return the leftover whenever one value is divided by another.

Syntax

var remainder = x % y;

The variable x is the numerator, and y is the dividend. The remainder (%) operator returns the remainder value of x and y.

Example 1

In this example, we have taken the (/) operator to get the quotient of two numbers. The remainder is found using the (%) operator.

Two variables, x1, and y1, have taken inputs from the user. The Math.floor() function round down the quotient value found using the quotient(/) operator. The remainder is calculated and printed on the screen. The x2-y2 pair shows a negative divisor and positive dividend division. The x3 variable takes a positive divisor as input, and y3 takes a negative dividend. The x4-y4 variables take a positive number as a dividend and zero as a divisor, yielding an infinity quotient and a NaN remainder.

<html> <body> <h3>Perform integer division and get remainder using <i>%operator</i> method</h3> <p id = "root"></p> </body> <script> let root = document.getElementById("root"); var x1 = 111; var y1 = 8; var quotient1 = Math.floor(x1/y1); var remainder1 = x1%y1; root.innerHTML = "111/8 =" +quotient1+ "<br>"; root.innerHTML += "111%8 = "+remainder1+"<br>"; var x2 = -127.7; var y2 = 0.144; var quotient2 = Math.floor(x2/y2); var remainder2 = x2%y2; root.innerHTML += "(-127.7)/0.144 = " + quotient2 + "<br>"; root.innerHTML += "(-127.7)%0.144 = "+ remainder2 + "<br>"; var x3 = 100.23; var y3 = -123.2; var quotient3 = Math.floor(x3/y3); var remainder3 = x3%y3; root.innerHTML += "(100.23)/(-123.2) = " + quotient3 + "<br>"; root.innerHTML += "(100.23)%(-123.2) = "+ remainder3 +"<br>"; var x4 = 14.9; var y4 = 0; var quotient4 = Math.floor(x4/y4); var remainder4 = x4%y4; root.innerHTML += "(14.9)/(0) = " +quotient4+ "<br>"; root.innerHTML += "(14.9)%(0) = "+remainder4+"<br>"; </script> </html>

Approach 2

The x and y are referred to as the dividend and the divisor for the operation x% y. If one of the operands is NaN, x is infinite, or y is zero, the operation returns NaN. Otherwise, the dividend x is returned if y is Infinity or x is 0.

The residual remainder is calculated when both operands are non-zero and finite. Here, (remainder= divisor - dividend * quotient), where the quotient is an integer with the same sign as the dividend x and is as close to 0 as possible.

We use the binary ~~ operator to find the quotient of the two numbers supplied. The remainder is calculated by multiplying the quotient by subtracting the divisor and the dividend. The parseInt() method returns the first integer after parsing a value as a string.

Syntax

var remainder = (x - y * parseInt(x / y));

The remainder can be calculated by subtracting the divisor from the dividend and multiplying the quotient by this value.

Example 2

In this example, the user has provided inputs to two variables, x1, and y1. These integers are both positive. The remainder is computed and shown on the screen.

The pair x2-y2 exhibits a negative dividend division and a positive dividend divisor. X3 accepts a positive dividend as input, while Y3 accepts a negative one. The x4-y4 variables divide by zero and accept a positive value as dividends, producing a NaN remainder and a quotient of 0. To preserve the true value of the quotient, the bitwise NOT operator is used twice.

<html> <body> <h3>Perform integer division and get remainder using <i>modulo</i> method</h3> <p id = "root"></p> </body> <script> let root = document.getElementById("root"); var x1 = 8; var y1 = 2; var quotient1 = ~~(x1/y1); var remainder1 = (x1 - y1 * parseInt(x1 / y1)); root.innerHTML = "8/2 =" +quotient1+ "<br>"; root.innerHTML += "8%2 = "+remainder1+"<br>"; var x2 = -77; var y2 = 0.87; var quotient2 = ~~(x2/y2); var remainder2 = (x2 - y2 * parseInt(x2 / y2)); root.innerHTML += "(-77)/0.87 = " +quotient2+ "<br>"; root.innerHTML += "(-77)%0.87 = "+remainder2+"<br>"; var x3 = 244.7; var y3 = -12.8; var quotient3 = ~~(x3/y3); var remainder3 = (x3 - y3 * parseInt(x3 / y3)); root.innerHTML += "(244.7)/(-12.8) = " +quotient3+ "<br>"; root.innerHTML += "(244.7)%(-12.8) = "+remainder3+"<br>"; var x4 = 122.11; var y4 = 0; var quotient4 = ~~(x4/y4); var remainder4 = (x4 - y4 * parseInt(x4 / y4)); root.innerHTML += "(122.11)/(0) = " +quotient4+ "<br>"; root.innerHTML += "(122.11)%(0) = "+remainder4+"<br>"; </script> </html>

In this tutorial, we have learned about two methods to perform integer division and get the remainder in JavaScript, i.e. by using the (%) operator and the modulo method.

raja
Updated on 14-Sep-2022 13:38:18

Advertisements