Integer Division Example

This example illustrates how to use JavaScript to do integer division. It uses the operators '/' (division) and '%' (modulus) to find the quotient and remainder due to integer division.

Note: JavaScript allows the use of the '%' operator on real values as well as integers (since all numeric values in JavaScript are actually floating-point values). The value shown for the remainder below should be meaningful for non-integer inputs in the following sense: The remainder is that portion of the numerator which is not accounted for by the product of the quotient and the denominator.

Note 2: The source code shows two methods for finding the quotient by integer division. The first reduces the numerator by the amount of the remainder before dividing by the denominator. The second uses the Math.floor() function (for non-negative quotients) or the Math.ceil() ["ceiling"] function (for negative quotients) to truncate the real quotient to an integer. (The quotient must be truncated towards 0, which is the floor function for positive reals and the ceiling function for negative reals.)

Enter a number to be used as a numerator:
Enter another number to be used as a denominator:


Copyright © 2000 -- Jonathan Mohr