Modulo Calculator

Calculate a mod b with quotient and remainder. Step-by-step explanation with clock arithmetic visual and negative number handling.

Ad
Ad

About Modulo Calculator

Calculate a mod b and see the quotient, remainder, and step-by-step working. Handles negative numbers correctly using mathematical modulo (always non-negative) and shows how this differs from the % operator in most programming languages.

What Is the Modulo Operation?

Modulo gives the remainder after integer division. The relationship is:

a = q × b + r where 0 ≤ r < b

Here q is the quotient (floor of a/b) and r is the remainder (a mod b).

Worked example: 17 mod 5

  1. Divide: 17 ÷ 5 = 3.4
  2. Floor quotient: q = 3
  3. Remainder: r = 17 - (3 × 5) = 17 - 15 = 2
  4. Result: 17 mod 5 = 2

Modulo Quick Reference

aba mod bQuotientWorking
2372323 - 3×7 = 23 - 21 = 2
1001248100 - 8×12 = 100 - 96 = 4
5082650 - 6×8 = 50 - 48 = 2
15503Evenly divisible
710707 < 10, so remainder is 7
3657152365 days = 52 weeks + 1 day

How Does Modulo Handle Negative Numbers?

This is where programming languages disagree. There are two conventions:

Convention-7 mod 3Used By
Mathematical (Euclidean)2Mathematics, this calculator
Truncated (% operator)-1JavaScript, C, C++, Java
Floored2Python, Ruby

Why the difference? It depends on how "quotient" is defined. Truncated division rounds toward zero (so -7/3 = -2), giving remainder -1. Floored division rounds down (so -7/3 = -3), giving remainder 2. Mathematical modulo guarantees the result is always between 0 and b-1.

Converting between them: If the % operator gives a negative result, add b to get the mathematical modulo: -1 + 3 = 2.

Clock Arithmetic - The Most Intuitive Example

A 12-hour clock is modulo 12 in action. Hours "wrap around" after 12:

24-Hour Timemod 1212-Hour Clock
0 (midnight)0 → 1212:00 AM
999:00 AM
1311:00 PM
1866:00 PM
231111:00 PM
25 (next day)11:00 AM

Days of the week work the same way with mod 7. If today is Wednesday (day 3) and you count forward 100 days: (3 + 100) mod 7 = 103 mod 7 = 5, which is Friday.

Common Uses of Modulo in Programming

Use CasePatternExample
Check even/oddn % 27 % 2 = 1 (odd), 8 % 2 = 0 (even)
Wrap array indexi % lengthIndex 7 in array of length 5 → position 2
Cycle through itemscounter % nAlternate 3 colours: counter % 3
Extract last digitn % 101234 % 10 = 4
Check divisibilityn % d == 0Is 45 divisible by 9? 45 % 9 = 0, yes
Hash table sizinghash % bucketsMap hash to bucket index
Format every nth itemi % n == 0Add line break every 5 items

Divisibility Rules Using Modulo

A number is divisible by d if and only if n mod d = 0:

TestCheckExample
Divisible by 2?n mod 2 = 0146 mod 2 = 0 → yes
Divisible by 3?n mod 3 = 0246 mod 3 = 0 → yes (digit sum = 12)
Divisible by 5?n mod 5 = 0345 mod 5 = 0 → yes
Divisible by 7?n mod 7 = 0343 mod 7 = 0 → yes (7³)

Modular Arithmetic in Cryptography

Modular arithmetic is fundamental to modern cryptography. RSA encryption relies on modular exponentiation: computing a^b mod n for very large numbers. Diffie-Hellman key exchange, digital signatures, and hash functions all use modular arithmetic extensively. The security comes from the fact that while computing a^b mod n is fast, reversing the operation (the discrete logarithm problem) is computationally infeasible for large numbers.

For full step-by-step division working, the long division calculator shows every bring-down step. For finding common factors and multiples, the GCF and LCM calculator uses prime factorisation.

All calculations run in your browser. No data is sent to any server.

Frequently Asked Questions

What is modulo?

Modulo (mod) gives the remainder after division. For example, 17 mod 5 = 2 because 17 divided by 5 is 3 with remainder 2.

How does modulo work with negative numbers?

The mathematical modulo always returns a non-negative result. So -7 mod 3 = 2 (not -1). This differs from JavaScript's % operator, which preserves the sign of the dividend.

What is clock arithmetic?

Clock arithmetic is a real-world example of modulo 12. After 12, the clock wraps around. So 15 o'clock on a 12-hour clock is 3 o'clock (15 mod 12 = 3).

How do I check if a number is divisible?

If a mod b equals 0, then b divides a evenly with no remainder. The calculator shows a 'Evenly divisible?' indicator for this.

Where is modulo used in programming?

Modulo is used for checking even/odd numbers, wrapping array indices, hash table indexing, formatting (every nth item), and time calculations.

Link to this tool

Copy this HTML to link to this tool from your website or blog.

<a href="https://toolboxkit.io/tools/modulo-calculator/" title="Modulo Calculator - Free Online Tool">Try Modulo Calculator on ToolboxKit.io</a>