Math

Modulo calculator (a mod n) with negative numbers

The modulo operation finds the remainder after dividing one number by another: 17 mod 5 = 2, because 17 = 5 × 3 + 2. This modulo calculator works with negative numbers and decimals and shows the three common conventions side by side, so you can match maths, Python, Excel, JavaScript or C.

Runs in your browser. Nothing you type is sent to us.

17 mod 5

2

Euclidean remainder, from 0 up to |n| − 1

Quotient (rounded down)
3
Remainder, truncated (C, Java, JavaScript %)
2
Remainder, floored (Python %, Excel MOD)
2
Remainder, Euclidean (never negative)
2
Check
5 × 3 + 2 = 17

Key facts

  • a mod n is the remainder r in a = n × q + r. For positive numbers, 17 mod 5 = 2.
  • With negative numbers, conventions differ: −17 mod 5 is 3 in maths, Python and Excel, but −2 in JavaScript, C and Java.
  • The Euclidean remainder is always between 0 and |n| − 1, which is what number theory and clock arithmetic use.
  • Clock arithmetic is modulo 12 or 24: 10 hours after 15:00 is (15 + 10) mod 24 = 1:00.
  • a mod n = 0 exactly when n divides a, which is how programmers test for even numbers (a mod 2 = 0).

How modulo works

Divide a by n, take the whole-number quotient q, and the remainder r is what is left:

a = n × q + r r = a − n × q

For 17 and 5: q = 3, r = 17 − 15 = 2. For 20 and 5: q = 4, r = 0, so 5 divides 20 exactly.

How to use the modulo calculator

Enter the dividend a and the divisor n. The main result is the Euclidean remainder (never negative). The list below it shows the quotient and the remainder under each convention, with a check that a = n × q + r.

Three conventions for negative numbers

The quotient can be rounded in different ways, and the remainder follows:

ConventionQuotient−17 mod 517 mod −5Used by
TruncatedRound toward zero−22C, C++, Java, JavaScript %, Go, Rust %
FlooredRound down3−3Python %, Excel MOD, Ruby, R %%
EuclideanChosen so r ≥ 032Number theory, many algorithms

The truncated remainder takes the sign of the dividend. The floored remainder takes the sign of the divisor. The Euclidean remainder is always non-negative.

Step-by-step examples: modulo

Example 1

17 mod 5 = 2 in every convention.

Example 2: negative dividend

−17 mod 5. Floored: q = ⌊−3.4⌋ = −4, r = −17 − 5 × (−4) = 3. Truncated: q = −3, r = −17 + 15 = −2.

Example 3: decimals

7.5 mod 2 = 1.5, because 7.5 = 2 × 3 + 1.5.

Example 4: clock time

It is 9 o’clock. What time will it be in 50 hours? (9 + 50) mod 24 = 59 mod 24 = 11, so 11:00.

Where modulo is used

  • Even and odd: n mod 2 is 0 for even numbers and 1 for odd numbers.
  • Cycles: days of the week (mod 7), months (mod 12), angles (mod 360°).
  • Check digits: ISBN-10 uses mod 11, IBANs use mod 97, and the Luhn algorithm for card numbers uses mod 10.
  • Programming: wrapping array indexes, hash tables, alternating row colours.
  • Cryptography: RSA and Diffie-Hellman rely on modular exponentiation with very large numbers.

Modular arithmetic rules

For any integers a, b and n > 0:

(a + b) mod n = ((a mod n) + (b mod n)) mod n(a × b) mod n = ((a mod n) × (b mod n)) mod n

These let you work with huge numbers by reducing at every step. For example, the last digit of 7¹⁰⁰ is 7¹⁰⁰ mod 10 = 1, because the last digits of powers of 7 cycle 7, 9, 3, 1.

Pitfalls with modulo

  • Assuming % in JavaScript or C gives a positive result for negative numbers.
  • Confusing modulo with division: 17 mod 5 is 2, not 3.4.
  • Using a divisor of zero, which is undefined.
  • Forgetting floating-point error when using modulo with decimals in code.

Modular arithmetic: clocks, check digits and cryptography

Carl Friedrich Gauss introduced the notation a ≡ b (mod n) in *Disquisitiones Arithmeticae* (1801), turning remainders into a full branch of number theory. The everyday example is the clock: 10 hours after 9 o’clock is 7 o’clock, because 19 mod 12 = 7. Days of the week work mod 7, which is why date algorithms such as Zeller’s congruence are modular.

Modular arithmetic quietly protects data. The ISBN-10 check digit is computed mod 11 (which is why some ISBNs end in X), ISBN-13 and barcodes use mod 10, and credit card numbers are checked with the Luhn algorithm, patented by IBM scientist Hans Peter Luhn in 1960. Public-key cryptography depends on it: RSA, published by Ron Rivest, Adi Shamir and Leonard Adleman in 1977, encrypts with modular exponentiation. Programming languages disagree about negative numbers: C and JavaScript truncate, so −7 % 3 = −1, while Python floors, giving 2. This calculator shows the main conventions so you can match your language. For remainders in long division, use the long division calculator; for primes behind RSA, the prime number calculator; and for greatest common divisors, the LCM and GCF calculator.

How this calculator works

  • a mod n = the remainder r with a = q × n + r.
  • Shows three conventions: Euclidean (r always ≥ 0), floored (r has the sign of n, as in Python) and truncated (r has the sign of a, as in C and JavaScript).
  • The floor quotient q and a check (q × n + r = a) are given.
  • n = 0 is undefined and rejected.

Frequently asked questions

What does mod mean in math?

a mod n is the remainder when a is divided by n. For example, 23 mod 4 = 3 because 23 = 4 × 5 + 3.

How do you calculate modulo?

Divide, keep the whole-number part of the quotient, multiply it back by the divisor and subtract from the dividend. 47 mod 6: 6 × 7 = 42, so the answer is 5.

What is −17 mod 5?

3 using the mathematical (floored or Euclidean) definition, as in Python and Excel. Languages that truncate, such as JavaScript and C, return −2.

Is modulo the same as remainder?

For positive numbers, yes. For negative numbers, “remainder” often means the truncated result, while “modulo” usually means the floored or Euclidean result.

What is 0 mod n?

0, for any non-zero n.

What is a mod 1?

For integers it is always 0. For decimals it gives the fractional part, for example 3.75 mod 1 = 0.75.

How do I get a positive modulo in JavaScript?

Use ((a % n) + n) % n, which turns the truncated result into the Euclidean one for positive n.

What is modulo used for?

Testing divisibility, wrapping around cycles like clocks and calendars, check digits, hashing and cryptography.

Sources

  1. Wolfram MathWorld, mod
  2. MDN Web Docs, remainder operator (%)
  3. Python documentation, binary arithmetic operations
  4. Microsoft Support, MOD function

Updated November 25, 2026. Formulas checked against published sources and worked examples.

Cite this page: CalcRoster. "Modulo calculator (a mod n) with negative numbers." calcroster.com/modulo-calculator/. Updated November 25, 2026.

Results are estimates for general information. They are not financial, medical or legal advice. Check important decisions with a qualified professional.