Skip to content
MasterMath

Modulo Calculator

The remainder of a division, shown both ways: the one programming languages give and the mathematical modulus, which differ for negatives.

Remainder

—

Remainder—
Mathematical modulus—
Whole quotient—
Is it divisible?—
Check—

How this was worked out

    The rule

    a mod b = a − b × ⌊a ÷ b⌋

    Why it works

    The modulo operation returns what is left after dividing. For positive numbers everyone agrees on the answer; for negatives there are two defensible conventions, and different languages picked different ones.

    How to do it by hand

    1. Divide and keep only the whole part of the quotient
    2. Multiply that back by the divisor
    3. Subtract from the dividend
    4. What is left is the remainder

    What is worth knowing

    The disagreement over negatives is real and worth knowing: −7 mod 3 is −1 in C, Java and JavaScript, and 2 in Python and Ruby. Both follow consistently from their definition of integer division, and neither is wrong. The mathematical convention is the second, because it keeps the result in the range 0 to b−1, which is what modular arithmetic needs. That property is why the mathematical version is what underlies clock arithmetic, hashing, checksums and every cryptographic algorithm that uses modular exponentiation.

    Frequently asked questions

    What is the modulo operation?

    The remainder left after dividing one number by another.

    Why do languages disagree about negatives?

    Because they define integer division differently. C-family languages truncate towards zero; Python floors towards negative infinity.

    Which convention is mathematically standard?

    The one that always returns a non-negative result, between 0 and the divisor minus one. That is what modular arithmetic requires.

    Where is modulo used?

    Clock arithmetic, hashing, checksums, cycling through an array, and the modular exponentiation at the heart of RSA.