Exact BigInt arithmeticReviewed 29 Sep 2026Runs in your browser

Modulo Calculator

Compute a mod n with the correct sign for negative numbers, check a congruence a ≡ b (mod n), find a modular inverse, or compute a large modular exponentiation aᵇ mod n exactly — every step shown.

Who wrote it

Written by Suhaib Hassan. Every worked example is re-derived by hand. Our methodology.

How the engine works

Modulo uses exact BigInt division; the modular inverse uses the extended Euclidean algorithm; exponentiation uses fast (binary) modular exponentiation, so even huge exponents stay exact and fast.

Why we built it

Programming languages disagree on the sign of a % b for negative numbers — this shows both conventions side by side.

a mod n

Presets
Expression—
a mod n—
—

—

Truncated remainder—
Quotient—

Where a lands on the number line mod n

Every multiple of n is marked; a wraps to its residue in [0, n).

—

Step-by-step working

—

—

    Floored vs. truncated modulo

    Languages disagree on the sign of the result when a is negative.

    ConventionFormula−7 mod 3Used by
    Floored (this calculator's default)a − n·⌊a/n⌋2Python %, mathematics
    Truncateda − n·trunc(a/n)−1C, Java, JavaScript %

    Worked examples

    Negative dividend

    What is −7 mod 3?

    −7 = 3 × (−3) + 2
    −7 mod 3 = 2 (floored)

    Congruence

    Is 17 ≡ 5 (mod 6)?

    17 mod 6 = 5, and 5 mod 6 = 5
    Yes — both sides give the same remainder

    Modular inverse

    Find 3⁻¹ mod 11.

    3 × 4 = 12 ≡ 1 (mod 11)
    3⁻¹ ≡ 4 (mod 11)

    Key definitions

    • Modulo: a mod n is the remainder left after dividing a by n, with 0 ≤ (a mod n) < n for n > 0 under the floored convention.
    • Congruence: a ≡ b (mod n) means n divides (a − b) exactly — a and b leave the same remainder.
    • Modular inverse: a⁻¹ mod n is the value x with a·x ≡ 1 (mod n); it exists exactly when gcd(a, n) = 1.
    • Modular exponentiation: aᵇ mod n, computed by repeated squaring so it stays fast even for huge b.

    By Suhaib Hassan · Reviewed 29 September 2026 · How CalculatePilot verifies formulas →

    Sources

    • Gauss, C. F. (1801). Disquisitiones Arithmeticae, Article 1 (the definition of congruence).
    • Knuth, D. E. (1997). The Art of Computer Programming, Vol. 2 (3rd ed.), section 4.5.2 (the Euclidean algorithm and modular inverses).
    • ISO/IEC 80000-2:2019, clause 9 (the floor function and integer division).

    Frequently asked questions

    Why is −7 mod 3 equal to 2, not −1?

    Under the mathematical (floored) convention, the result of a mod n always has the same sign as n (here positive), so it stays in [0, n). −7 = 3 × (−3) + 2, giving remainder 2. Some programming languages instead truncate toward zero, giving −1 — this calculator shows both.

    When does a modular inverse not exist?

    Exactly when gcd(a, n) ≠ 1 — that is, a and n share a common factor. For example, 4 has no inverse mod 8, since gcd(4, 8) = 4.

    How is aᵇ mod n computed for a huge exponent b?

    By fast (binary) exponentiation: repeatedly squaring a mod n and multiplying in the factors corresponding to the binary digits of b, taking the modulus at every step. This needs only about log₂(b) multiplications, so even a 1000-digit exponent computes instantly.

    What is the modulo operation used for?

    Clock and calendar arithmetic, hash tables, checksums, cryptography (RSA relies on modular exponentiation), pseudo-random number generators, and cycling through array indices.

    Is a mod 0 defined?

    No — division by zero is undefined, so a mod 0 has no value. This calculator flags it as an error rather than returning a result.

    How is congruence different from equality?

    Equality means two numbers are identical. Congruence mod n is weaker: it only requires them to leave the same remainder when divided by n, so infinitely many numbers (…, b−n, b, b+n, b+2n, …) are all congruent to b.

    Related calculators

    Continue with number theory.

    All Math Calculators

    Embed This Modulo Calculator

    Free and responsive, and it runs in the visitor's browser. Branding removal from $7.99/month.

    <iframe src="https://www.calculatepilot.com/embed/modulo-calculator.html" width="100%" height="560" loading="lazy" title="Modulo Calculator"></iframe>
    For website owners

    Want this calculator on your own website?

    Embed it in one line of code. Keep it free with a small CalculatePilot attribution link, or go white-label: your brand, no attribution, one license for all your domains.

    • All 159 calculators, unlimited embeds
    • Works on WordPress, Webflow, Squarespace, Wix and plain HTML
    • Runs in your visitor's browser, so nothing is sent to a server
    • License key emailed automatically after checkout, cancel anytime
    $7.99 / month
    or $89.99 / year · white-label Pro
    Get the white-label license Embed free with attribution