Modulo Calculator
Calculate the remainder of a division - clearly explains how negative operands are handled, since modulo behavior differs across languages.
Enter two numbers - this tool calculates the remainder and explains the result, especially for negative operands where behavior varies by language.
When both operands are positive, these are identical. With a negative dividend, JavaScript's % (and most C-family languages) returns a result with the same sign as the dividend - the "true" mathematical modulo (as used in Python) always returns a result with the same sign as the divisor. Both are shown so you can see the difference clearly.
More Data / Number / Computer Science Tools
View all 19JavaScript's % operator (which this tool uses) returns a result with the same sign as the dividend - some languages (like Python) instead return a result with the same sign as the divisor. Both are documented below.
Modulo vs. Remainder - Why Negatives Matter
For positive numbers, "modulo" and "remainder" always agree. For negative operands, they can differ: JavaScript, C, and Java's % operator gives a result with the same sign as the dividend (true "remainder" semantics), while Python's % gives a result with the same sign as the divisor (true "modulo" semantics).
How to Use It
- Enter a dividend and a divisor.
- View the result using JavaScript/remainder semantics, alongside the Python-style modulo result for comparison when they'd differ.
Handling Division by Zero
Division/modulo by zero is mathematically undefined - this tool reports that clearly rather than returning a misleading NaN with no explanation.
Examples
| Expression | JavaScript / C / Java (%) | Python (%) |
|---|---|---|
| 17 mod 5 | 2 | 2 |
| -17 mod 5 | -2 | 3 |
| 17 mod -5 | 2 | -3 |
| 10 mod 2 | 0 | 0 |
Everyday Uses
- Even or odd:
n % 2 === 0means even. - Wrapping around: the day of the week 10 days after Friday is
(5 + 10) % 7. - Time: 135 minutes is 2 hours and
135 % 60= 15 minutes. - Alternating rows: style every third row with
index % 3 === 0. - Hashing and sharding:
id % 4picks one of four servers or buckets.
Getting a Positive Result in JavaScript
To get Python-style results (always matching the divisor's sign), use ((a % n) + n) % n. For example, ((-17 % 5) + 5) % 5 is 3 - useful when wrapping array indexes backwards.
For percentages, use the Percentage Calculator; for bit-level maths, the Bitwise Calculator.
Frequently Asked Questions
Explore More Tools
Keep going with related categories and our most used tools.