Modulo Calculator

Compute a mod b for any integers, positive or negative. See the quotient, remainder and step-by-step working — with both the mathematical and the programming sign conventions.

What is modulo?

The modulo operation returns the remainder after dividing one integer by another. Written a mod b, it answers: "after taking out as many whole copies of b as fit inside a, what's left over?"

Formally, given integers a and b ≠ 0, we write a = q · b + r with 0 ≤ r < |b|. That r is the value of a mod b. A familiar case is clock arithmetic — if it's 10 AM now, 15 hours later is (10 + 15) mod 12 = 1. The clock forgets everything except the remainder.

Modulo, piece by piece

Each card explains one thing this tool decides for you — how the remainder is defined, how it handles negative inputs, and why the two common conventions can disagree.

Definition — how the remainder is chosen

Write a = q · b + r and pick the integer quotient q that makes 0 ≤ r < |b|. That r is a mod b — the "leftover" after taking out whole copies of b.

a mod b = a − ⌊a / b⌋ · b
3 whole b's fitr = a − 3b17 mod 5: three 5's, remainder 2
Example
Given
a = 17, b = 5
Substitute
17 − ⌊17/5⌋ · 5 = 17 − 3 · 5
Answer
2

Negative dividends — sign follows the divisor

For negatives we still require 0 ≤ r < |b|, so the mathematical result has the same sign as the divisor b. The tool follows this convention.

a mod b ≡ a + k·b (mod b)
pick k
0 ≤ r < |b|
-9-6-30369−7−7 + 3·3 = +2 (mathematical)result has sign of divisor b
Example
Given
a = −7, b = 3
Substitute
⌊−7/3⌋ = −3 → r
= −7 − (−3)·3
Answer
2

Math vs programming — where they disagree

Many programming languages truncate the quotient toward zero instead of flooring, which flips the sign of the remainder for negative inputs. The tool shows both so you can match the behaviour of your language.

truncated r = a − trunc(a / b) · b
−7 ÷ 3Math (floor)q=−3, r=+2Python, RubyTruncatedq=−2, r=−1C, Java, JS
Example
Given
−7 mod 3
Substitute
floor → +2
truncate → −1
Answer
+2 (math) · −1 (C/Java/JS)

Features of this calculator

  • Handles positive, negative and mixed-sign integer inputs
  • Follows the mathematical convention (result has the sign of the divisor)
  • Also shows the truncated result used by C, Java and JavaScript
  • Displays the quotient and full a = q · b + r identity
  • Step-by-step working using the floor definition

Frequently asked questions

+What is the modulo operator used for?

It returns the remainder after integer division and is used for cyclic problems: array wrap-around, calendar arithmetic, hashing, cryptography and checksums.

+Why is −7 mod 3 different in math vs programming?

They use different definitions of the quotient. The mathematical convention floors, giving quotient −3 and remainder 2. Truncated modulo (C, Java, JS) rounds toward zero, giving quotient −2 and remainder −1.

+Can the divisor be zero?

No. Division by zero is undefined, so a mod 0 has no meaning either.

+What if both operands are negative?

The mathematical result still has the sign of the divisor. For example, −7 mod −3 = −1, because −7 = 2 · (−3) + (−1) and −1 is in the range (−3, 0].

Related calculators