Math

Modulo Calculator

Find remainder of division. Fast, accurate, and completely free.

Favorites

Recent Tools

Modulo Operation (a mod b)

Results

Mathematical Formula

a = b \times q + r,\quad 0 \le r < |b|

a = dividend (the number being divided)

b = divisor (the number dividing)

q = quotient (integer result of division, floored)

r = remainder (a mod b), satisfying 0 ≤ r < |b|

How to Use this Calculator

  1. Enter the dividend (a) — the number you want to divide.

  2. Enter the divisor (b) — the number to divide by. Must not be zero.

  3. Click Calculate to see the quotient, remainder, and the division algorithm verification.

  4. For negative numbers, compare truncated (JavaScript/C style) vs. floored (Python/math style) remainder behavior.

The Modulo Operation Explained

The modulo operation is one of the most useful yet often misunderstood operations in mathematics and computer science. At its core, the modulo operation finds the remainder after integer division — but its implications extend far beyond simple arithmetic into cryptography, hash functions, circular data structures, and number theory. This calculator helps you explore modulo with full support for negative numbers and clear visualization of the division algorithm.

The Division Algorithm

The foundation of the modulo operation is the division algorithm, which states that for any integer a (the dividend) and any nonzero integer b (the divisor), there exist unique integers q (the quotient) and r (the remainder) such that a = b times q + r, where the remainder satisfies 0 less than or equal to r less than the absolute value of b. This theorem is not just a formula — it guarantees that every integer division has a unique, well-defined quotient and remainder pair.

Modulo vs. Remainder

While often used interchangeably in casual conversation, "modulo" and "remainder" can produce different results when negative numbers are involved. The key distinction lies in how the quotient is computed. Truncated division (used by JavaScript, C, and Java with the % operator) rounds the quotient toward zero, which can produce negative remainders. Floored division (used by Python and the mathematical definition) rounds the quotient toward negative infinity, ensuring the remainder is always non-negative when the divisor is positive.

Negative Number Behavior

Understanding modulo with negative numbers is crucial for programmers. Consider -7 mod 3: using truncated division (JavaScript), -7 / 3 truncates to -2, giving a remainder of -7 - (3 times -2) = -1. Using floored division (Python), -7 / 3 floors to -3, giving a remainder of -7 - (3 times -3) = 2. Both are mathematically valid, but they follow different conventions. This calculator shows both results side by side so you can understand the difference and choose the appropriate convention for your use case.

Applications in Computer Science

The modulo operation is ubiquitous in programming. Hash tables use modulo to map keys to bucket indices. Circular buffers use it to wrap array indices. Clock arithmetic is inherently modular — 15:00 plus 10 hours equals 1:00 (25 mod 24 = 1). CSS animations and game loops use modulo for repeating patterns. Even simple tasks like determining if a number is even or odd use modulo: n mod 2 equals 0 for even numbers.

Applications in Mathematics

In number theory, modular arithmetic forms an entire branch of mathematics. Two numbers are said to be congruent modulo n if they have the same remainder when divided by n. This concept underpins the RSA cryptographic algorithm, the Chinese Remainder Theorem, Fermat's Little Theorem, and many other foundational results. Modular arithmetic also appears in checksum algorithms (like ISBN verification), calendar calculations (determining the day of the week for any date), and error-correcting codes used in data transmission.

Common Pitfalls

Several common mistakes occur when working with modulo. First, division by zero is undefined — a mod 0 has no meaning. Second, confusing the truncated and floored remainder conventions leads to bugs in programs that handle negative numbers. Third, assuming that a mod b always has the same sign as a is incorrect (it depends on the language). Fourth, forgetting that modulo does not distribute over addition in the way multiplication does can lead to algebraic errors. This calculator handles all these cases correctly and provides clear explanations.

  • Clock Arithmetic: 14 + 13 hours = 3 o'clock (27 mod 24 = 3)
  • Even/Odd Check: n mod 2 = 0 means n is even
  • Hash Functions: key mod tableSize determines bucket placement
  • Cryptography: RSA encryption relies heavily on modular exponentiation

Frequently Asked Questions (FAQ)

What is the difference between modulo and remainder?

For positive numbers, they produce identical results. The difference appears with negative numbers. The remainder (truncated division, used by JavaScript and C) can be negative, while the mathematical modulo (floored division, used by Python) always returns a non-negative result when the divisor is positive. For example, -7 % 3 is -1 in JavaScript but 2 in Python.

Why can't I divide by zero?

Division by zero is mathematically undefined. There is no number q such that 0 times q equals a nonzero dividend, and for a zero dividend, every number satisfies the equation, making the result indeterminate. The modulo operation inherits this restriction since it depends on division.

How does modulo work with negative dividends?

It depends on the convention. With truncated division (JavaScript, C, Java), the result has the same sign as the dividend: -7 % 3 = -1. With floored division (Python, Ruby), the result has the same sign as the divisor: -7 % 3 = 2. This calculator shows both conventions so you can compare.

What is the division algorithm?

The division algorithm states that for any integer a and positive integer b, there exist unique integers q (quotient) and r (remainder) such that a = b times q + r, where 0 is less than or equal to r and r is less than b. It guarantees that every division has a unique quotient-remainder pair.

Where is modulo used in real life?

Modulo is used extensively in computing and daily life: determining if a number is even or odd (n mod 2), clock arithmetic (hours mod 12 or 24), hash table indexing, cryptographic algorithms like RSA, checksum validation (ISBN, credit card numbers), calendar calculations, and circular data structures like ring buffers.

Can modulo be used with decimal numbers?

While mathematically possible, the modulo operation is traditionally defined for integers. Some programming languages support floating-point modulo (e.g., 5.5 % 2.0 = 1.5 in JavaScript), but results can be imprecise due to floating-point representation. This calculator focuses on integer modulo for clarity and precision.

Explore more tools you might find useful