Modulo Calculator
Compute the remainder (modulo) and quotient of a division, with both the truncated and floored conventions shown so the negative-number cases are unambiguous.
Como usar esta calculadora
- 1Enter the dividend, the number being divided.
- 2Enter the divisor, the number you're dividing by.
- 3Read the remainder under both the floored and truncated conventions.
- 4For positive inputs the two agree; check both when either number is negative.
Como funciona
Modulo
a mod n = a − n · ⌊a ÷ n⌋ (floored) remainder = a − n · trunc(a ÷ n) (truncated) the two agree when a and n share a sign floored result lies in [0, n) for n > 0
The modulo operation gives the remainder left over when one number is divided by another. Formally, a mod n is what remains after subtracting the largest whole multiple of n that fits into a. For positive numbers this is unambiguous and matches the remainder learned in primary-school division. The subtlety appears with negatives, where two conventions diverge. The truncated convention rounds the quotient toward zero and gives a remainder with the sign of the dividend, which is what C, Java, and JavaScript's % operator do. The floored convention rounds the quotient down (toward negative infinity) and gives a remainder with the sign of the divisor, which is what Python uses and what mathematicians mean by modulo — it always yields a result between 0 and n−1 for a positive divisor. Knowing which one a language or context uses prevents a whole class of bugs.
Exemplo resolvido
17 mod 5 = 2, because 5 goes into 17 three times (15) with 2 left over. The signs match, so both conventions agree. But −17 mod 5 is 3 under the floored (Python) convention and −2 under the truncated (C/Java) convention — the same division, two different remainders, depending on which rule the tool follows.
Modulo Calculator: o guia completo
What modulo means and why it's everywhere
The modulo operation answers a simple question: after dividing evenly as many times as possible, what is left? Twenty-three divided by five is four with three remaining, so 23 mod 5 is 3. That leftover turns out to be extraordinarily useful, because it captures cyclical, wrap-around behaviour. Clock arithmetic is modulo 12 or 24; days of the week are modulo 7; angles wrap at modulo 360. Any time a count needs to loop back to the start, modulo is the natural tool.
In computing, modulo is one of the most heavily used operations. It decides whether a number is even or odd (n mod 2), distributes items evenly across buckets in hash tables, wraps array indices around, generates repeating patterns, and drives much of cryptography, where modular arithmetic on enormous numbers secures communications. Its ubiquity is why every programming language has a modulo operator — and why the differences between their conventions matter so much in practice.
The negative-number gotcha
For positive numbers, everyone agrees on the remainder. The trouble starts with negatives, and it trips up even experienced programmers. Consider −17 mod 5. One reasonable answer is −2, obtained by dividing toward zero (−17 ÷ 5 rounds to −3, and −17 − (−3 × 5) = −2). Another equally reasonable answer is 3, obtained by rounding the division down to −4 (−17 − (−4 × 5) = 3). Both satisfy the defining property that quotient times divisor plus remainder equals the dividend; they simply make different choices about which way to round the quotient.
Languages split along exactly this line. C, C++, Java, JavaScript, and Go use truncated division, so their % gives a remainder matching the dividend's sign — −2 in the example. Python, Ruby, and most of pure mathematics use floored division, giving a remainder matching the divisor's sign — 3 in the example, always non-negative for a positive divisor. Neither is wrong, but code that assumes one and runs on the other produces subtle, hard-to-spot errors, especially in things like wrapping an index that occasionally goes negative. The safe habit is to know your language's convention and, when you need a non-negative result, to normalise explicitly.
Getting the result you actually want
Because of the convention split, a common practical need is a remainder that is always non-negative — the true mathematical modulo — regardless of the language. The reliable formula for this is ((a mod n) + n) mod n: take whatever remainder your language gives, add the divisor to lift any negative into positive territory, and take the modulo once more to bring it back into range. This guarantees a result between 0 and n−1 for a positive n, which is usually what wrap-around logic assumes.
This matters most in situations like cycling through a fixed set of positions. Imagine stepping backward through the seven days of the week: naively computing (dayIndex − steps) mod 7 in a truncated-remainder language can produce a negative index that crashes or misbehaves, whereas the floored result correctly wraps around to the end of the week. The floored value this calculator highlights is the one that behaves intuitively for such cyclical problems, which is why it is shown as the primary result while the truncated value is offered alongside for those working in C-family languages.
Perguntas frequentes
What is the modulo operation?
Modulo gives the remainder after dividing one number by another. 17 mod 5 = 2, because 5 divides into 17 three times with 2 left over. It captures cyclical behaviour — clocks, weekdays, angles — and is one of the most-used operations in programming.
Why do calculators give different answers for negative modulo?
Because of two conventions. Truncated division (C, Java, JavaScript) gives a remainder with the sign of the dividend, so −17 mod 5 = −2. Floored division (Python, mathematics) gives one with the sign of the divisor, so −17 mod 5 = 3. Both are valid; they round the quotient differently.
How do I always get a positive remainder?
Use ((a mod n) + n) mod n. Adding the divisor lifts any negative remainder into positive range, and the final modulo brings it back within 0 to n−1. This gives the true mathematical modulo regardless of your language's convention, which is what wrap-around logic usually needs.
What is modulo used for?
Checking even or odd (n mod 2), wrapping values around a cycle like clock hours or array indices, distributing items across hash-table buckets, generating repeating patterns, and modular arithmetic in cryptography. Anywhere a count needs to loop back to the start, modulo is the tool.