Modulo Calculator

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

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.

Ad
Ad

About Modulo Calculator

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 Properties

Modular arithmetic obeys consistent rules that let you simplify large expressions before computing a final mod. These identities, formalised by Carl Friedrich Gauss in his 1801 work Disquisitiones Arithmeticae, are the foundation of modern number theory.

PropertyIdentityWhy It Matters
Addition(a + b) mod n = ((a mod n) + (b mod n)) mod nReduce intermediate values to keep numbers small
Subtraction(a - b) mod n = ((a mod n) - (b mod n)) mod nWorks the same in reverse - watch for negative results
Multiplication(a × b) mod n = ((a mod n) × (b mod n)) mod nPrevents overflow when computing products of large numbers
Exponentiationa^k mod n = ((a mod n)^k) mod nUsed in RSA - lets you compute a^65537 mod n without ever storing a^65537
Congruencea ≡ b (mod n) if n divides (a - b)Two numbers are "equivalent" under mod n

Worked example: To find (47 × 83) mod 10 without multiplying: 47 mod 10 = 7, 83 mod 10 = 3, then (7 × 3) mod 10 = 21 mod 10 = 1. Check: 47 × 83 = 3,901, and 3,901 mod 10 = 1. Same answer, far less work.

Modular Arithmetic in Cryptography

Modular arithmetic is the backbone of modern cryptography. RSA encryption (specified in RFC 8017 and standardised by NIST in FIPS 186-5) relies on modular exponentiation: computing a^b mod n where n is a 2,048-bit or 3,072-bit number. The security rests on the fact that computing a^b mod n is fast with the square-and-multiply algorithm, while reversing it (the discrete logarithm or integer factorisation problem) is computationally infeasible for large n.

Cryptographic PrimitiveHow Modulo Is UsedTypical Modulus Size
RSAc = m^e mod n (encryption), m = c^d mod n (decryption)2,048-3,072 bits
Diffie-HellmanShared secret = g^(ab) mod p2,048-4,096 bits
Elliptic Curve (ECDSA)All point operations computed mod prime p256-521 bits
HMAC / Hash functionsIndex mixing, finite-field rotations32-64 bits per operation
Post-quantum (CRYSTALS-Kyber)Polynomial arithmetic mod q = 3,32912 bits per coefficient

Why Do Programming Languages Disagree About Negative Modulo?

Because the C standard (ISO/IEC 9899) defined integer division as truncating toward zero, every language that inherited C semantics - JavaScript, Java, C++, Go, Rust, C# - returns a remainder with the sign of the dividend. Python, Ruby, and most computer-algebra systems picked floored division instead, so their % operator always returns a non-negative result when the divisor is positive. Neither is "wrong" - both satisfy the division algorithm (a = q × b + r), but they pick different q values.

Language-7 % 3Convention
JavaScript, Java, C, C++, Rust, Go-1Truncated (C standard)
Python, Ruby, Haskell (mod), Perl2Floored
Haskell (rem), OCaml-1Truncated (like C)
Microsoft Excel MOD()2Floored
Mathematical convention (this calculator)2Euclidean (always non-negative)

Practical safe pattern in JavaScript: ((a % n) + n) % n. This converts any truncated result into the mathematical (non-negative) modulo, which is what you usually want when wrapping array indices, cycling colours, or working with clock arithmetic. For deeper number-theory work involving primes and factorisation, pair this with the prime factorisation tool.

Common Mistakes and How to Avoid Them

A few traps catch both students and working programmers:

  • Assuming % is always non-negative. In JavaScript, -1 % 7 returns -1, not 6. Wrapping a pagination index with a naive modulo leaves you one step off the end of the list. Fix: ((i % len) + len) % len.
  • Dividing by zero. All major languages throw or return NaN when the divisor is 0. Always validate the divisor before computing mod.
  • Floating-point modulo. JavaScript's % works on floats (5.5 % 2 = 1.5), but accumulated rounding can make (0.1 + 0.2) % 0.1 return 0.09999999999999998, not 0. Use integer modulo whenever possible, or compare against a small epsilon.
  • Confusing mod with absolute remainder. 17 mod 5 = 2, not 3. The quotient floors, it does not round to nearest.
  • Overflow in C/C++. Computing (a * b) % n with 32-bit ints and large a, b overflows before the mod is applied. Use 64-bit intermediates or modular multiplication identities (see properties table above).

How Does Modulo Relate to Other Number Operations?

Modulo sits at the centre of elementary number theory alongside GCD, LCM, and prime factorisation. Euclid's algorithm for the greatest common divisor is defined entirely in terms of modulo: gcd(a, b) = gcd(b, a mod b), terminating when b = 0. This single identity, recorded in Euclid's Elements Book VII around 300 BCE, is still the fastest known general-purpose GCD method. For exponents, the exponent calculator handles raw a^b, and combined with modulo you get the modular exponentiation that powers RSA. Fermat's Little Theorem (a^(p-1) ≡ 1 mod p for prime p) and Euler's theorem generalise this further.

Where Does Modulo Appear in Everyday Software?

Outside cryptography and mathematics, modulo is one of the most heavily used operators in real-world code. A 2023 GitHub analysis of top-1,000 starred repositories found the % operator in roughly 68% of JavaScript and Python codebases, typically for the patterns below:

  • Hash tables: Map a 64-bit hash to one of N buckets with hash % N. This is why hash-table sizes are often chosen as primes - it reduces clustering when hashes have patterns.
  • Pagination: Compute current page from an absolute index: Math.floor(i / pageSize) plus i % pageSize for the offset within the page.
  • Animation timing: Looping animations use (Date.now() / period) % 1 to get a 0-to-1 progress value that repeats forever.
  • Round-robin load balancing: Request n goes to server n % serverCount.
  • Check digits: The Luhn algorithm (credit cards, ISINs) and ISBN-13 both end with a mod 10 or mod 97 check. UK IBANs use mod 97.
  • Time formatting: Seconds to MM:SS is Math.floor(s/60) + ':' + (s % 60).toString().padStart(2, '0').

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

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

Sources

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>