The Standard Engine.
MathematicsFinanceHealthPhysicsEngineeringBrowse all

Everyday Life · General Mathematics

Modulo Calculator

Computes the remainder left over after dividing one integer by another using the modulo operation.

Calculator

Advertisement

Formula

Here, a is the dividend (the number being divided), m is the modulus (the divisor), \lfloor \cdot \rfloor denotes the floor function (rounding down to the nearest integer), and the result a mod m is the non-negative remainder after dividing a by m.

Source: Knuth, D.E. — The Art of Computer Programming, Vol. 1: Fundamental Algorithms (3rd ed.), Addison-Wesley, 1997.

How it works

The modulo operation, written as a mod m, returns the non-negative remainder after dividing the dividend a by the modulus m. Unlike ordinary division which returns a decimal, modulo isolates only the leftover part. For example, if you divide 17 by 5, you get 3 with a remainder of 2, so 17 mod 5 = 2. This seemingly simple operation underpins a vast range of mathematical structures including cyclic groups, congruences, and hash functions.

The formula is defined as a mod m = a − m · ⌊a/m⌋, where ⌊a/m⌋ is the floor of the quotient — the largest integer less than or equal to the true division result. This floor-based definition ensures the result is always non-negative even when the dividend a is negative, which distinguishes mathematical modulo from the remainder operator (%) in some programming languages such as C or Java that can return negative values for negative inputs. Languages like Python, Ruby, and most mathematical contexts follow the floor-division convention producing a non-negative result.

Practical uses of modulo arithmetic are ubiquitous. Checking whether a number is even uses n mod 2: a result of 0 means even, 1 means odd. Clock arithmetic is modulo 12 or 24 — after hour 23 comes hour 0, not hour 24. Calendar calculations use modulo to determine days of the week. In computer science, hash tables use modulo to map large keys into fixed-size arrays. Cryptographic algorithms such as RSA and Diffie-Hellman are built entirely on modular exponentiation. Digital checksums and error-detection codes like ISBN validation rely on modulo 10 or modulo 11.

Worked example

Suppose you want to calculate 47 mod 6.

Step 1 — Identify values: Dividend a = 47, Modulus m = 6.

Step 2 — Compute the floor quotient: 47 ÷ 6 = 7.8333..., so ⌊47/6⌋ = 7.

Step 3 — Multiply: 6 × 7 = 42.

Step 4 — Subtract: 47 − 42 = 5.

Therefore, 47 mod 6 = 5. You can verify this by confirming that 6 × 7 + 5 = 47. ✓

Now consider a negative example: −13 mod 4.

Step 1: a = −13, m = 4.

Step 2: −13 ÷ 4 = −3.25, so ⌊−13/4⌋ = −4 (floor rounds toward negative infinity).

Step 3: 4 × (−4) = −16.

Step 4: −13 − (−16) = 3.

So −13 mod 4 = 3. Note that a C-style % operator would return −1 instead, which is why the mathematical definition matters.

Limitations & notes

This calculator uses the mathematical floor-division definition of modulo, which always returns a non-negative result. If you need the truncation-based remainder (as in C, Java, or JavaScript's % operator), the results will differ for negative dividends. The modulus m must be a non-zero integer; division by zero is undefined and will produce no result. While the formula works formally for non-integer real numbers (e.g. 5.5 mod 2.2), the classical modulo operation is defined for integers and results may be less meaningful for floating-point inputs due to precision limitations. Very large integers may be subject to floating-point rounding errors in browser-based calculations.

Frequently asked questions

What is the difference between modulo and remainder?

Modulo and remainder are closely related but differ for negative numbers. The mathematical modulo operation always returns a non-negative result by using the floor of the quotient. The remainder operation in many programming languages (like C or Java's % operator) uses truncation toward zero, which can return a negative value when the dividend is negative. For example, −13 mod 4 = 3 using floor division, but −13 % 4 = −1 using truncation. Python follows the mathematical floor convention, while C and Java use truncation.

What does a result of zero mean in modulo?

A result of zero means the dividend is exactly divisible by the modulus with no remainder. For instance, 15 mod 5 = 0 because 5 divides 15 exactly three times. In programming, this is commonly used to check divisibility: if n mod k equals 0, then n is divisible by k. This is how you test for even numbers (n mod 2 = 0) or multiples of any number.

How is modulo used in clock arithmetic?

Clock arithmetic is a classic real-world example of modular arithmetic. A 12-hour clock operates modulo 12: if it is currently 10 o'clock and you add 5 hours, you get 15 mod 12 = 3 o'clock. A 24-hour clock works modulo 24. This cyclical wrapping behavior is also used to calculate days of the week, months, and periodic events in scheduling algorithms.

Can modulo be applied to non-integer numbers?

Formally, the modulo operation is defined for integers in number theory. However, the floor-division formula can be extended to real numbers — for example, 7.5 mod 2.5 = 0. In practice, floating-point precision issues can cause unexpected results with decimals. For most everyday and programming uses, modulo is applied to integers. If you need real-number modulo, proceed with caution and verify results manually.

How is modulo used in cryptography and computer science?

Modulo is foundational in cryptography. The RSA encryption algorithm relies on modular exponentiation: computing a^e mod n for very large integers. The Diffie-Hellman key exchange uses modular arithmetic to securely share keys over public channels. In computer science, hash tables use h(key) = key mod table_size to map keys to array indices. Cyclic redundancy checks (CRC) and checksum algorithms like ISBN-13 validation (mod 10) also depend on modulo operations.

Last updated: 2025-01-15 · Formula verified against primary sources.