terican

Last verified · v1.0

Calculator · math

Modulo Calculator

Calculate the remainder of any division instantly. Enter dividend and divisor to compute a mod n using floored, truncated, or Euclidean convention.

FreeInstantNo signupOpen source

Inputs

Remainder (a mod n)

Explain my result

0/3 free

Get a plain-English breakdown of your result with practical next steps.

Remainder (a mod n)

The formula

How the
result is
computed.

What Is the Modulo Operation?

The modulo operation returns the remainder after dividing one integer by another. Given a dividend a and a divisor n, the result r satisfies the equation:

r = a mod n = a − n · ⌊a ÷ n⌋

For example, 17 mod 5 = 2, because 17 = 5 × 3 + 2. The modulo operation appears throughout number theory, computer science, and cryptography — from clock arithmetic to RSA encryption. Mastering the uses of modulo calculator tools starts with understanding this foundational definition.

Formula Derivation

The formula is derived directly from the Division Algorithm: for any integers a and nonzero n, there exist unique integers q (quotient) and r (remainder) such that a = n · q + r, with the constraint that 0 ≤ r < |n|. Solving for r gives:

r = a − n · ⌊a / n⌋

Here, ⌊·⌋ denotes the floor function, which rounds toward negative infinity. This floored definition is the standard used by Python and aligns with the approach described in Khan Academy's introduction to modular arithmetic. The rigorous number-theoretic treatment appears in Evan Dummit's lecture notes on modular arithmetic in ℤ.

The Three Modulo Conventions

Different programming languages and mathematical traditions handle negative inputs differently. This calculator supports all three major conventions:

  • Floored (Python / Khan Academy standard): The remainder always shares the sign of the divisor n. The quotient rounds toward −∞. Example: −7 mod 3 = 2, because ⌊−7 / 3⌋ = −3, so −7 − 3 × (−3) = 2.
  • Truncated (C / Java / JavaScript standard): The remainder shares the sign of the dividend a. The quotient truncates toward 0. Example: −7 mod 3 = −1, because trunc(−7 / 3) = −2, so −7 − 3 × (−2) = −1.
  • Euclidean: The remainder is always non-negative, regardless of the signs of a or n. Formula: r = a − |n| · ⌊a / |n|⌋. Example: −7 mod 3 = 2. Formal congruence theory covering this convention is available in UC Irvine's notes on congruences and congruence equations.

For positive values of both a and n, all three conventions produce identical results. Differences appear only when at least one operand is negative.

Variable Reference

  • a — Dividend: The integer being divided. Accepts any positive, negative, or zero integer value.
  • n — Divisor / Modulus: The number that a is divided by. Must be nonzero; a zero modulus makes the operation undefined.
  • r — Remainder: The output of the operation. Its sign and range depend on the chosen convention.

Worked Examples

Example 1: Positive Integers

Compute 29 mod 6: quotient = ⌊29 / 6⌋ = 4; remainder = 29 − 6 × 4 = 5. Verification: 6 × 4 + 5 = 29. All three conventions agree on this result.

Example 2: Negative Dividend

Compute −13 mod 5 under each convention:

  • Floored: ⌊−13 / 5⌋ = −3 → −13 − 5 × (−3) = 2
  • Truncated: trunc(−13 / 5) = −2 → −13 − 5 × (−2) = −3
  • Euclidean: ⌊−13 / 5⌋ = −3 → −13 − 5 × (−3) = 2

Real-World Uses of the Modulo Calculator

  • Clock arithmetic: To find the hour 50 hours after 9:00 AM, compute (9 + 50) mod 12 = 59 mod 12 = 11 — the answer is 11:00 AM.
  • Cryptography: RSA encryption computes ciphertext as me mod n, where n is the product of two large primes. The security of the scheme rests on the difficulty of reversing modular exponentiation.
  • Day-of-week calculation: Zeller's congruence uses mod 7 to determine the weekday for any historical or future date, given the day, month, and year.
  • Hash tables: Programs map arbitrary keys to fixed-size array indices via the formula index = key mod tableSize, spreading data evenly across memory.
  • Cyclic sequences: Repeating patterns — carousel indices, pixel color wrapping, round-robin scheduling — use modulo to keep values within a fixed range without branching logic.
  • Divisibility testing: A number a is divisible by n if and only if a mod n = 0. For instance, 144 mod 12 = 0 confirms that 144 is exactly divisible by 12.

Reference

Frequently asked questions

What does a modulo calculator do?
A modulo calculator computes the remainder left over after dividing one integer by another. Enter a dividend a and a divisor n, and the tool instantly returns r = a mod n. For example, 17 mod 5 = 2 because 17 divided by 5 gives quotient 3 with remainder 2. The calculator also supports floored, truncated, and Euclidean sign conventions for negative numbers.
What is the difference between floored, truncated, and Euclidean modulo?
The three conventions differ only when negative numbers are involved. Floored modulo (Python) gives a remainder with the same sign as the divisor: -7 mod 3 = 2. Truncated modulo (C, Java, JavaScript) gives a remainder with the same sign as the dividend: -7 mod 3 = -1. Euclidean modulo always returns a non-negative remainder regardless of input signs: -7 mod 3 = 2. For strictly positive inputs all three conventions produce identical results.
What are the most common uses of a modulo calculator?
Common uses of a modulo calculator include clock arithmetic (finding the time after N hours), day-of-week calculation via Zeller's congruence (mod 7), hash table indexing in data structures (key mod tableSize), RSA and Diffie-Hellman cryptographic operations, cycling through array indices without overflow, and divisibility testing — if a mod n = 0, then n divides a exactly with no remainder.
Why does -7 mod 3 give 2 in Python but -1 in JavaScript?
Python uses the floored convention: the quotient rounds toward negative infinity. floor(-7 / 3) = -3, so the remainder is -7 - 3 * (-3) = 2. JavaScript uses the truncated convention: the quotient truncates toward zero. trunc(-7 / 3) = -2, so the remainder is -7 - 3 * (-2) = -1. Both results are mathematically consistent within their own convention; the difference reflects a deliberate language design choice rather than an error in either system.
Can the modulus n be a negative number?
Yes, n can be negative, though results vary by convention. Under the truncated convention, -13 mod -5 = -3, matching the sign of the dividend. Under the Euclidean convention, the formula uses |n|, so the sign of n does not change the result: -13 mod -5 = 2. In most programming and applied mathematics contexts a positive modulus is assumed, but formal number theory fully defines and permits negative divisors.
How is modular arithmetic used in cryptography?
Modular arithmetic underpins public-key cryptography. RSA encryption computes ciphertext as m raised to the power e, mod n, where n is the product of two large prime numbers. Diffie-Hellman key exchange relies on the computational hardness of the discrete logarithm problem in modular arithmetic. These operations are fast to compute in the forward direction but computationally infeasible to reverse without the private key, forming the security basis of modern encrypted communications.