Modulo Calculator

Part of Math & Statistics Calculators

Calculate the remainder when dividing two numbers. Compute a mod b with detailed breakdown.

What is the Modulo Operation?

The modulo operation (often written as "mod" or represented by the % symbol in programming) finds the remainder after division of one number by another. For example, 17 mod 5 = 2 because when you divide 17 by 5, you get a quotient of 3 with a remainder of 2. The modulo operation returns that remainder value.

Mathematically, for integers a and b where b ≠ 0, the expression "a mod b" represents the remainder r when a is divided by b, where 0 ≤ r < |b|. The relationship can be expressed as: a = (q × b) + r, where q is the quotient and r is the remainder (the result of a mod b).

How Modulo Works

To calculate a mod b manually:

  1. Divide a by b to get the quotient (ignoring any decimal part).
  2. Multiply the quotient by b.
  3. Subtract this product from a to get the remainder.

For example, to calculate 23 mod 7: 23 ÷ 7 = 3 remainder 2. So 23 = (3 × 7) + 2, therefore 23 mod 7 = 2.

How to Use This Calculator

  1. Enter the dividend (a): This is the number being divided. It can be positive or negative.
  2. Enter the divisor (b): This is the number you're dividing by. It must be non-zero.
  3. Calculate: Click "Calculate a mod b" to see the remainder along with the quotient and full division breakdown.
  4. View details: The calculator shows both the quotient and remainder, plus the formula verification.

Programming Language Implementations

JavaScript/TypeScript: a % b - Note: In JavaScript, the result's sign matches the dividend's sign for negative numbers.

Python: a % b - Python's modulo always returns a non-negative result when the divisor is positive.

Java/C/C++: a % b - The result's sign matches the dividend in these languages.

Ruby: a.modulo(b) or a % b - Ruby's behavior matches Python, ensuring non-negative results.

Different programming languages handle negative numbers differently in modulo operations. This calculator uses the mathematical definition where the result is always non-negative when the divisor is positive.

Common Use Cases

Determining odd/even numbers: n mod 2 returns 0 for even numbers and 1 for odd numbers. This is one of the most common uses in programming.

Circular arrays and rotation: When wrapping around arrays or lists, (index + offset) mod length ensures the index stays within bounds.

Clock arithmetic: Converting 24-hour time to 12-hour format uses modulo 12. For example, 15:00 becomes 15 mod 12 = 3 (3:00 PM).

Hashing algorithms: Hash functions often use modulo to map large values into a fixed range of bucket indices.

Cryptography: Modular arithmetic is fundamental to many encryption algorithms, including RSA, which relies on modulo operations with very large prime numbers.

Modular Arithmetic Properties

Addition: (a + b) mod m = ((a mod m) + (b mod m)) mod m

Subtraction: (a - b) mod m = ((a mod m) - (b mod m)) mod m

Multiplication: (a × b) mod m = ((a mod m) × (b mod m)) mod m

These properties allow you to reduce large numbers before performing operations, which is crucial for working with very large numbers in cryptography and computer science. By reducing numbers modulo m at each step, you can avoid integer overflow issues.

Special Cases and Edge Cases

Zero dividend: 0 mod b = 0 for any non-zero b.

Dividend smaller than divisor: When a < b (both positive), a mod b = a. For example, 3 mod 7 = 3.

Equal numbers: a mod a = 0 for any non-zero a.

Negative numbers: The mathematical definition ensures that if b > 0, then 0 ≤ (a mod b) < b, even when a is negative. For example, -3 mod 5 = 2 because -3 = (-1 × 5) + 2.

Modulo operations are used in the GCD Calculator via the Euclidean algorithm. The Binary Calculator and Hex Calculator also use modular arithmetic for base conversions.