StatGardenREF. DESK
Calculators/Maths/Modulo & integer division
Maths

Modulo & integer division calculator

Quotient, remainder and the true mathematical modulo.

What this calculator does

The modulo operation gives the remainder after division. It sounds trivial, but it underpins a surprising amount: cyclical arithmetic, hash tables, checksums, cryptography, and every calculation that wraps around a fixed range like clock time or days of the week.

Negative numbers are where implementations disagree. The mathematical convention keeps the result non-negative, so −7 mod 5 is 3. Most programming languages, including JavaScript, keep the sign of the dividend and return −2 instead. This calculator shows both, because which one you want depends entirely on what you are doing.

The formula

Formulaa mod n = a − n⌊a/n⌋ (always non-negative); JavaScript % keeps the sign of a

The truncated remainder is what most languages compute, taking the sign of the dividend. The mathematical modulo subtracts the floor of the quotient rather than its truncation, which is what makes the result non-negative for positive divisors. The two agree whenever the dividend is positive.

TermMeaning
DividendThe number being divided.
DivisorThe number you are dividing by.
RemainderWhat is left over.
Floor quotientThe quotient rounded toward negative infinity.
Truncated quotientThe quotient rounded toward zero.

The inputs explained

FieldWhat to enter
Dividend aThe dividend. Negative values are handled, and the two remainder conventions will differ.
Divisor nThe divisor. Cannot be zero.

When to use it

Wrapping around a cycle

Working out the day of the week 100 days from now is 100 mod 7. Anything that repeats on a fixed cycle reduces to modulo arithmetic.

Testing divisibility

A remainder of zero means the division is exact. This is how even and odd are tested, and how the prime factorisation algorithm decides whether a factor divides.

Distributing items into buckets

Hash tables, load balancers and round-robin schedulers all use modulo to map an arbitrary index onto a fixed number of slots.

Handling negative indices

Wrapping a negative position back into a valid range needs the mathematical modulo, not the truncated one. This is a frequent source of bugs when moving code between languages.

Worked examples

Every figure in the tables below is produced by this page’s own calculator at build time, so the numbers and the tool always agree. Select any row to load that scenario.

Dividing by 5

A range of dividends, including negatives where the two conventions diverge.

Divisor 5
DividendRemainder (a mod n)Truncated remainderInteger quotientFloor quotient
-73.000-2.000-1-2
-14.000-1.000-0-1
00000
72.0002.00011
122.0002.00022
250055
For −7 the mathematical modulo gives 3 while the truncated remainder gives −2. They differ by exactly the divisor, and only ever disagree when the dividend is negative.

The same dividend across divisors

Seventeen divided by a range of divisors.

Dividend 17
DivisorRemainder (a mod n)Integer quotientDivides evenly?
21.0008No
32.0005No
52.0003No
81.0002No
1701Yes
2017.0000No
17 is prime, so it divides evenly only by 1 and itself. When the divisor exceeds the dividend, the remainder is simply the dividend.

Questions

What is the difference between mod and remainder?

They agree for positive numbers. For negatives, the mathematical modulo returns a non-negative result while the remainder keeps the sign of the dividend. −7 mod 5 is 3 mathematically but −2 in most programming languages.

How do I get a non-negative result in code?

Use ((a % n) + n) % n. Adding the divisor and taking the modulo again shifts any negative result back into the correct range.

What is modulo used for?

Cyclical arithmetic, hash functions, checksums such as the Luhn algorithm on card numbers, random number generation, and most of public-key cryptography, which is built on modular exponentiation.

Can the divisor be negative?

Mathematically yes, though the conventions become murkier. Most practical uses involve a positive divisor, and that is where the two definitions have their clearest meaning.

What happens if I divide by zero?

It is undefined. The calculator cannot return a meaningful result, and every programming language will either throw an error or produce a special not-a-number value.

For factors and divisibility, see prime factorisation or GCD and LCM.