Modulo Calculator
Result
Congruence
- Remainder
- 3
The modulo of one whole number by another is the smallest non-negative remainder left when the first is divided by the second, and this page writes it out as a congruence: -47 ≡ 3 (mod 5). The residue is always at least zero and strictly less than the size of the modulus, whatever the signs of the two inputs. That single rule is what makes modular arithmetic work with negative numbers at all. Ask a programming language for -47 % 5 and the answer depends on the language: JavaScript, C, Java and Go all return -2, while Python returns 3. Only one of those is the mathematical residue, and it is the one that never comes out negative. The page takes two whole numbers between minus one billion and one billion, and it prints both the residue and the equation that ties the dividend, the residue and the modulus together. Nothing else is required of the inputs: the dividend may be negative, the modulus may be negative, and the dividend may be smaller than the modulus in size. The only value that is refused is a modulus of zero, because no number is divisible by zero and mod 0 therefore has no answer to give.
Four sign combinations, with the congruence each one produces
| Dividend | Modulus | Residue | Congruence |
|---|---|---|---|
| -47 | 5 | 3 | -47 ≡ 3 (mod 5) |
| -1 | 2 | 1 | -1 ≡ 1 (mod 2) |
| 47 | 5 | 2 | 47 ≡ 2 (mod 5) |
| 47 | -5 | 2 | 47 ≡ 2 (mod -5) |
Read the first and third rows together: the dividend is -47 in one and 47 in the other, the modulus is 5 in both, and the residues are 3 and 2. That pair is the whole point of the page. Under the convention most calculators and programming languages use, the first row would read -2, and the fact that it reads 3 instead is the difference between a remainder and a residue. Now read the first and second rows: the dividend stays negative while the modulus is halved from 5 to 2, and the residue goes from 3 to 1 — halving the modulus does not halve the residue, it re-measures the same number against a shorter cycle. The fourth row has a modulus of -5 and answers 2, exactly as the third row with a modulus of 5 does, which is the sign-insensitivity in one comparison. Check any of the four by subtracting the residue from the dividend: the result is always a whole multiple of the modulus, which is what the congruence is claiming.
Formula
-47 ÷ 5 = -9 remainder -2 (truncated); -2 + 5 = 3, so -47 mod 5 = 3 and -47 ≡ 3 (mod 5)
- a
- The dividend, the number being reduced. It may be negative, and that is the case worth testing: a mod n has to come out non-negative even when a does not, so -47 has to land on 3 rather than on -2. Whole numbers from -1000000000 to 1000000000 are accepted, and 0 is a perfectly good dividend whose residue is always 0
- n
- The modulus, the number being divided by. It may be negative too, and the residue does not change when it is: 47 mod -5 and 47 mod 5 are both 2, because the statement n divides a - r and the statement -n divides a - r are two ways of saying the same thing. Zero is the one value refused, and the page prints n exactly as it was typed rather than replacing it with its size
- r
- The residue, which is the answer. It is the smallest non-negative number that can be taken away from a and leave a multiple of n. Two conditions pin it down completely: r is at least 0, and r is strictly less than the size of n. -47 mod 5 gives 3 because 3 is the smallest non-negative value for which -47 - 3 = -50 is a multiple of 5
- ((a mod n) + |n|) mod |n|
- The two-step recipe the page follows, and the reason the second step is there. The first step is what most languages compute and it can hand back a negative answer; the second step adds the size of the modulus and reduces again, which pushes the result into the non-negative interval without changing which multiple of n it is measured from. Using the size of n rather than n itself is what lets a negative modulus pass through unchanged
- a ≡ r (mod n)
- The congruence, which is the first line of the result panel. It says that n divides a - r exactly, and it is the same statement as the residue written on its own, with the modulus and the dividend named alongside it. The triple-bar symbol and the mod keyword are notation rather than words, so they are printed identically in every language on the site, and no thousands separators are inserted into the numbers
- -47 ≡ 3 (mod 5)
- The default input written out in full. It is the case that separates this page from a remainder calculator: the same two numbers give -9 remainder -2 under the truncated convention that a four-function calculator uses, and -47 ≡ 3 (mod 5) here. Feeding 47 instead of -47 gives 2 rather than 3, so a single minus sign moves the answer by one whole step around the cycle
Reach for this page whenever the answer has to wrap around rather than stop. Clock arithmetic is the oldest example: 10 o'clock plus 5 hours is 3 o'clock, which is 15 mod 12, and the residue never coming out negative is exactly what keeps a clock face readable. Calendar work has the same shape — day 100 of a year, or the weekday a given date falls on, is a residue modulo 7. In programming, a modulo is how you test whether one number divides another (a mod n is 0 when n goes in exactly), how you fold an index back into range in a circular buffer or a hash table, how you keep a looping counter inside fixed bounds, and how you pick an item from a list in turn. Cryptography is built on it: RSA and Diffie-Hellman are modular arithmetic on very large numbers, and the whole reason the residue is defined to be non-negative is that a signed answer would be ambiguous arithmetic to build on. Check digits, from bank account numbers to ISBNs, are residues modulo 9, 10 or 11. When the question is instead 'how many times does it go in, and what is left over, under the convention my calculator uses', the remainder calculator is the better page because it lays both conventions side by side; when the question is what the two numbers have in common, the greatest common factor page answers that directly.
Worked examples
The sign trap: -47 mod 5
- Divide as usual and throw away the fraction: -47 ÷ 5 = -9.4, so the truncating quotient is -9
- -9 × 5 = -45, and -47 - (-45) = -2, so the truncated remainder is -2
- The residue has to be at least 0, so add the size of the modulus: -2 + 5 = 3
- Check the result sits in range: 0 ≤ 3 < 5, so no further adjustment is needed
- Read the congruence: -47 ≡ 3 (mod 5), meaning 5 divides -47 - 3 = -50 exactly
The default input, and the one that explains why this page exists. A four-function calculator, and most programming languages, will report -9 remainder -2 for this division; that answer is not wrong, it is answering a differently worded question. The residue here is 3 because 3 is the smallest non-negative value that leaves a multiple of 5. Notice that 47 mod 5 is 2, not -3 and not 3 — moving the dividend by one whole step around the cycle moves the residue by one, and the two residues differ by exactly 1 rather than by 5.
A negative modulus: 47 mod -5
- 47 ÷ 5 = 9.4, so 5 goes in nine times and 9 × 5 = 45
- 47 - 45 = 2, which already sits in the interval 0 ≤ 2 < 5
- The modulus is negative, but divisibility does not care about the sign: -5 divides 45 exactly, so it divides 47 - 2 exactly too
- The congruence is written with the modulus as it was entered: 47 ≡ 2 (mod -5)
The case that shows the residue is insensitive to the sign of the modulus. Replacing -5 with 5 changes nothing about the answer, because -5 divides a number exactly when 5 does. The page keeps the minus sign in the printed congruence rather than quietly dropping it; rewriting a -5 as 5 would look like the input had been discarded. Compare the two rows in the reference table that share the divisor -5 with rows that share 5: only the dividend's sign ever moves the residue.
The plain case: 1234 mod 12
- 12 × 100 = 1200, and 1234 - 1200 = 34, so carry on: 12 × 2 = 24 leaves 34 - 24 = 10
- 12 × 3 = 36 would overshoot 34, so the quotient is 102 and the leftover is 10
- 10 is at least 0 and less than 12, so it is already the residue
- Read it as a congruence: 1234 ≡ 10 (mod 12)
The plain case with two positive numbers, where the residue and an ordinary remainder agree — which is the point of including it. When both numbers are positive there is nothing to reconcile, and the page is doing the same work any remainder tool does. The interesting rows are the ones with a minus sign in them. This particular pair has a second reading worth noticing: 1234 mod 12 is how you would ask which hour a duration lands on when the clock has twelve hours on its face, and 10 is a perfectly sensible hour while -2 would not have been.
Limitations
The modulus may not be zero. Dividing by zero has no answer, so there is no remainder to report and no least non-negative one at that; the page refuses the input rather than returning a placeholder that looks like a result. Both numbers must be whole. A fractional modulus has its own definition in some branches of mathematics, but it is not the one this page implements, so 47.5 is refused rather than quietly rounded to 48 and answered as if that were what you asked. The magnitude of each number is capped at one billion, which keeps every intermediate step inside the range where a double-precision number still represents integers exactly; beyond that the arithmetic would start rounding and the answer would look perfectly ordinary while being wrong. The page reports a residue and a congruence and nothing else: it does not give the quotient, does not list the other numbers congruent to your dividend, and does not do modular arithmetic on more than one pair at a time. A negative modulus is accepted and printed as entered, which means two congruences that say the same thing can look different on the page — 47 ≡ 2 (mod -5) and 47 ≡ 2 (mod 5) are the same statement. Finally, the reference table below shows four fixed pairs rather than following your inputs; the result panel is the part that answers what you typed.
Frequently asked questions
- Why is -47 mod 5 equal to 3 and not -2?
- Because the residue is defined to be the smallest non-negative number that can be subtracted from the dividend to leave a multiple of the modulus. Both -2 and 3 qualify in the sense that -47 - (-2) = -45 and -47 - 3 = -50 are multiples of 5, so the arithmetic alone does not choose between them. The definition does: the residue lives in the interval from 0 up to but not including the modulus, and -2 is outside it. Everything else follows. What you get from a programming language depends on which convention it chose, and JavaScript, C, Java and Go chose the other one — they let the sign follow the dividend. Python chose this one. Neither language is broken; they answer differently worded questions.
- Can the modulus be negative?
- Yes, and the residue is unaffected by its sign. A negative modulus works because divisibility ignores signs: -5 divides a number exactly when 5 does, so the set of multiples of -5 is the same set as the multiples of 5, and the smallest non-negative number in that set is the same either way. The page prints the modulus exactly as entered rather than replacing -5 with 5, because rewriting your input would look like the sign had been dropped. So 47 mod -5 is 2 and 47 mod 5 is 2 as well, and the two printed congruences differ only in that one shows a minus sign.
- What happens if I enter 0 as the modulus?
- The page refuses it. Mod 0 has no value to report: zero divides only zero, so there is no whole multiple of 0 to measure the dividend against and no remainder to take. Returning 0, or the dividend itself, would look like an answer and would be wrong for every input. Refusing the input is the honest outcome. Dividing by zero is undefined everywhere else in arithmetic for the same reason.
- Is this the same as the remainder calculator on this site?
- No, and the two give different answers to the same sum, which is the clearest way to see the difference. The remainder page lays out two conventions side by side and lets you choose, because long division genuinely has two of them in circulation and a classroom may teach either. This page does not offer a choice, because modulo has only one reading: the answer is never negative. For -47 divided by 5 the remainder page will show -9 remainder -2 under its default convention, while this page shows the congruence -47 ≡ 3 (mod 5). Use that page to see how the two conventions differ on a specific pair of numbers; use this one when what you want is the residue itself.
- Where is a residue actually used?
- Wherever a count has to wrap around rather than keep growing. A clock face is the everyday case: twelve hours on the dial means the hour after 10 o'clock plus 5 hours is 15 mod 12, which is 3. Weekdays work the same way modulo 7. In programming, a modulo tests divisibility because a mod n is 0 exactly when n goes into a evenly; it folds an index back into range in a circular buffer or a hash table, where an index that has run off the end has to come back to the start; and it keeps a looping counter inside fixed bounds. Public-key cryptography is modular arithmetic on very large numbers, which is why the non-negative convention matters — arithmetic that produced signed results would be ambiguous to build on.
- Why is only the congruence shown and not the quotient?
- Because the quotient is not part of what a residue says. A congruence names the dividend, the residue and the modulus, and the claim it makes is that the modulus divides the difference between the first two. That statement is complete without a quotient, since the quotient is the number you get by asking how many whole times the modulus fits, and that question belongs to long division. If you want the quotient alongside, the remainder calculator and the long division calculator both report it. The residue on its own is also printed, on the second row of the result panel, so a reader who only wants the number does not have to read it out of the sentence.
References
- Modular Arithmetic — the arithmetic of remainders, including the convention that the residue is taken in the interval from 0 up to the modulus — Wolfram MathWorld (United States)
- Congruence — the notation a ≡ r (mod n) and what it means for one number to divide the difference of two others exactly — Wolfram MathWorld (United States)
- Remainder (%) — the JavaScript operator whose sign follows the dividend, the behaviour that makes -47 % 5 return -2 and the reason a second step is needed here — MDN Web Docs (Mozilla)
- 教育部关于印发义务教育课程方案和课程标准(2022 年版)的通知——The fifth item in the annex list of this notice is the Mathematics Curriculum Standards for Compulsory Education (2022 edition); division with a remainder and congruence are part of the Number and Algebra strand from the upper primary grades into lower secondary, and the grade-band requirements are governed by the text of that annex, and the wording of the standards and the grade-band breakdown are governed by that annex — 中华人民共和国教育部