Math

Modulo Calculator

Calculate the modulo of any two numbers, with the negative-operand behaviour that differs between languages.

Need this done properly for your business?

Radiatus delivers secure cloud, DevOps & compliance engineering.

Book a free consult

Modulo returns what is left over

17 mod 5 is 2, because 5 goes into 17 three times with 2 remaining. It is the companion of integer division: the quotient is how many whole times, the modulo is what did not fit. Together they reconstruct the original, which is the identity any implementation must satisfy.

Negative operands are where languages diverge

−7 mod 3 is 2 in Python and −1 in C, Java and JavaScript. Both are defensible: Python's result always takes the sign of the divisor, while C-family languages truncate division toward zero and let the remainder take the sign of the dividend. Neither is wrong, and code ported between them breaks silently on negative inputs. When the result must always be non-negative, the portable fix is ((a % n) + n) % n.

What it is used for

Wrapping values into a range is the main one: hours on a clock, days of the week, positions in a circular buffer. Testing divisibility, since a mod n of 0 means n divides a evenly. Distributing work across N workers by hashing a key and taking the modulo. Extracting digits, since n mod 10 gives the last one. Alternating behaviour, since i mod 2 distinguishes odd from even.

Modular arithmetic underpins cryptography

RSA, Diffie-Hellman and elliptic curve cryptography all operate in modular arithmetic, because it produces a finite space where exponentiation is easy and its inverse is not. Hash tables use it to map arbitrary hashes into a fixed number of buckets, and checksum algorithms including the Luhn check on card numbers are modular arithmetic in plain form.

Modulo by a power of two is a bit mask

x mod 8 equals x AND 7, because a power of two boundary is a clean bit split. Compilers apply this automatically for unsigned types, which is one reason hash table sizes are often powers of two. For signed types the substitution is invalid without care, precisely because of the negative-operand behaviour above.

Modulo bias in random selection

Taking a random number modulo a range skews the distribution unless the range divides the generator's period evenly, because the leftover values at the top make some outcomes marginally more likely. For a die roll this is undetectable; for cryptographic selection it narrows the effective space. Rejection sampling — discarding values in the unusable tail — removes it, and most modern libraries expose a correct range function that does this internally.

Frequently Asked Questions

Privacy & Security

All processing happens locally in your browser — nothing is uploaded.

Data: None
Client-side-Side
Active
v1.0

How to Use

Enter the dividend and divisor to get the remainder and quotient.

Disclaimer: This tool is provided "as is" without warranty of any kind. Results are for educational and utility purposes.