Arithmetic • Core Pillar

Modulo Calculator

The definitive modular arithmetic calculator for calculating standard residues (a mod n), negative modulo transformations, fast modular exponentiation, and modular multiplicative inverses.

|
Last Updated: September 2026
|
Verified Mathematical Solution

Enter Modulo Parameters

a mod n
Worked Example Presets:

Mathematical Division Theorem Form

17 = (5 × 3) + 2

The canonical mathematical modulo operation always yields a non-negative residue 0 ≤ r < n, calculated via floored integer division: r = a − n × ⌊a/n⌋.

Modulo Output Canonical Residue
Canonical Remainder (r)
2
17 ≡ 2 (mod 5)
Dividend (a) 17
Modulus (n) 5
Quotient (q) 3
Prog. Rem (%) 2
Equivalence Class [r] modulo n:
{..., -13, -8, -3, 2, 7, 12, 17, 22, ...}

Modular Clock / Ring Representation

ℤ/nℤ

Clock arithmetic modulo 5: traversing 17 positions lands on 2.

Step-by-Step Proof:
Direct Answer & Overview
Verified Educational Guide

How to Calculate Modulo (a mod n)

The modulo operation (abbreviated 'mod') computes the integer remainder left over after dividing one integer (the dividend a) by another (the modulus n). In canonical number theory, the remainder r is strictly non-negative (0 ≤ r < n), calculated via floored division: r = a - n × ⌊a/n⌋.

Primary Mathematical Formula Standard Mathematical Model
Standard Equation
ƒ(x)
Q.E.D.
a mod n = r ⟺ a = (q × n) + r, where 0 ≤ r < n
Evaluated with exact mathematical formulation • Rigorously verified
Exact Formula
Input Parameters
Required
1
Dividend (a): The integer number being divided or wrapped around the modular circle (positive or negative)
2
Modulus (n): The positive base integer defining the cycle length (n > 0)
Expected Outputs
Calculated
Canonical Residue (r): The non-negative remainder in the range [0, n - 1]
Modular Congruence: Statement a ≡ r (mod n)
Equivalence Class: The infinite set of congruent integers {..., r - n, r, r + n, ...}
Worked Numerical Example
Instant Verification
Calculate 17 mod 5
→ 17 ÷ 5 = 3 with remainder 2 ⟹ 17 = (5 × 3) + 2
2

Modular Arithmetic Fundamentals & Clock Math

Modular arithmetic is a system of arithmetic for integers where numbers "wrap around" upon reaching a certain fixed value—the modulus (n). It is intuitively known as clock arithmetic because a standard 12-hour clock wraps back to 1 after reaching 12.

For example, if it is currently 9:00 and 5 hours pass, simple addition gives 9 + 5 = 14. On a 12-hour clock, we calculate 14 mod 12 = 2, so the clock reads 2:00.

The Modulus (n)
Base Cycle Size

The total number of unique states in the ring ℤ/nℤ, strictly {0, 1, 2, ..., n-1}.

Congruence Class [r]
a ≡ b (mod n)

All integers sharing the exact same remainder when divided by n (e.g. 2, 7, 12, 17 mod 5).

Canonical Residue
0 ≤ r < n

The unique non-negative representative of the equivalence class lying strictly within the base ring.

Mathematical vs Programming Modulo (Handling Negative Numbers)

One of the most frequent sources of bugs in computer programming is the distinction between Mathematical Modulo (Floored Division) and Programming Remainder (Truncated Division) when dealing with negative dividends:

Mathematical Modulo (Floored) Python, Ruby, LaTeX
-17 mod 5 = 3

Uses floored division (⌊-17 / 5⌋ = -4). Remainder is calculated as -17 - (5 × -4) = -17 + 20 = 3. The result is always guaranteed to be non-negative (0 ≤ r < 5).

Truncated Remainder (%) C, C++, Java, JS
-17 % 5 = -2

Truncates division towards zero (trunc(-17 / 5) = -3). Remainder is calculated as -17 - (5 × -3) = -17 + 15 = -2. To fix this in JavaScript or C++, use ((a % n) + n) % n.

Core Modular Arithmetic Algebraic Rules

Modular arithmetic preserves standard algebraic identities under addition, subtraction, multiplication, and exponentiation:

  • • Modular Addition: (a + b) mod n = [(a mod n) + (b mod n)] mod n
  • • Modular Subtraction: (a − b) mod n = [(a mod n) − (b mod n) + n] mod n
  • • Modular Multiplication: (a × b) mod n = [(a mod n) × (b mod n)] mod n
  • • Modular Exponentiation: (ab) mod n = [(a mod n)b] mod n

Real-World Applications: Cryptography & Computing

Public-Key Cryptography

RSA encryption and Diffie-Hellman key exchange rely on the one-way trapdoor function of modular exponentiation and prime moduli.

Hash Tables & Data Structures

Hash functions map arbitrary keys into fixed-size array slots by evaluating hash(key) mod table_size.

Checksums & Barcodes

Credit card validation (Luhn Algorithm mod 10), ISBN-10 book codes (mod 11), and IBAN bank numbers (mod 97) prevent data transmission errors.

Step-by-Step Worked Examples

Standard Positive Modulo Level: Basic

Calculate 125 mod 12.

1. Divide: 125 ÷ 12 = 10.4166... → Integer quotient q = 10.

2. Multiply: 12 × 10 = 120.

3. Subtract: 125 − 120 = 5.

Result: 125 mod 12 = 5 ⇔ 125 ≡ 5 (mod 12).

Negative Dividend Modulo Level: Intermediate

Calculate -25 mod 7.

1. Compute floored division: ⌊-25 / 7⌋ = ⌊-3.571⌋ = -4.

2. Multiply: 7 × (-4) = -28.

3. Subtract: -25 − (-28) = -25 + 28 = 3.

Result: -25 mod 7 = 3 ⇔ -25 ≡ 3 (mod 7). (Notice 0 ≤ 3 < 7).

Common Calculation Pitfalls & Division in Modular Math

Pitfall: Dividing Directly in Modular Arithmetic

You cannot simply divide numbers modulo n. Instead of dividing by a, you must multiply by its modular multiplicative inverse a-1, which only exists if gcd(a, n) = 1.

Pitfall: Modulo of Negative Differences

When computing (a - b) mod n where b > a, remember to add n before the final modulo to avoid negative residue errors.

Fact-Checked & Verified • Computational Accuracy Standards
Updated July 2026 • Editorial Policy
Authored By
Sanjay Samanta

Lead Developer & Founder of Basic Math Tools. Specializes in browser-native computational algorithms and applied mathematics.

Reviewed & Verified By
Academic Review Board

Mathematics & curriculum specialists. Audited against standard algebraic and arithmetic principles.

Found an error or have an improvement suggestion? Report a calculation issue

Frequently Asked Questions

What is the modulo operation and what does a mod n mean?
The modulo operation (written as a mod n) calculates the remainder left over when an integer a (the dividend) is divided by an integer n (the modulus). For example, 17 mod 5 = 2 because 5 fits into 17 three whole times with 2 left over (17 = 5 × 3 + 2).
How do you calculate the modulo of a negative number in mathematics versus programming?
In pure mathematics and number theory, the canonical modulo is always non-negative (0 ≤ r < n), calculated via floored division: r = a - n × ⌊a/n⌋ (e.g., -17 mod 5 = -17 - 5 × (-4) = 3). In many programming languages like C, C++, Java, and JavaScript, the % operator uses truncated division towards zero and returns a negative remainder (-17 % 5 = -2). Python and Ruby use mathematical floored modulo.
What is modular congruence (a ≡ b mod n)?
Two integers a and b are said to be congruent modulo n (written as a ≡ b (mod n)) if they have the same remainder when divided by n, or equivalently, if their difference (a - b) is an exact integer multiple of n (i.e., n divides (a - b)). For example, 17 ≡ 2 (mod 5) because 17 - 2 = 15, which is divisible by 5.
What is modular exponentiation (a^b mod n) and why is it important?
Modular exponentiation computes the remainder when a base a raised to a power b is divided by n ((a^b) mod n). Because calculating huge numbers like 7^100 directly causes arithmetic overflow, modular exponentiation uses repeated squaring (square-and-multiply) to reduce intermediate values modulo n at each step. This is the cryptographic engine powering RSA, Diffie-Hellman, and modern SSL/TLS security.
What is a modular multiplicative inverse (a⁻¹ mod n)?
The modular multiplicative inverse of an integer a modulo n is an integer x such that (a × x) ≡ 1 (mod n). It acts as the modular equivalent of division. A unique inverse exists if and only if a and n are coprime (gcd(a, n) = 1), computed using the Extended Euclidean Algorithm.
Can the modulus n ever be zero or negative?
Modulo by zero (a mod 0) is mathematically undefined because division by zero is impossible. While a negative modulus n can mathematically define a residue class modulo |n|, the standard modulus in algebra, cryptography, and computer science is defined as a strictly positive integer (n ≥ 1).