Modulo Calculator

Your values

The number being divided

The modulus; it cannot be 0

Whole numbers of any size are exact; terminating decimals such as 5.5 also work.

Result

−7 mod 3

−7=3×(−3)+2-7=3\times({-3})+2

The conventions disagree because a negative number is involved; compare them in the table.

Quotient and remainder by convention

ConventionQuotient qRemainder r
TruncatedC, C++, Java, JavaScript %, C#, Go, Rust %−2−1
FlooredPython %, Excel and Google Sheets MOD, Ruby, R %%−32
EuclideanNumber theory, Rust rem_euclid; never negative−32

Check: dividend equals divisor times quotient plus remainder

−7=3×(−2)+(−1)-7=3\times({-2})+({-1})
−7=3×(−3)+2-7=3\times({-3})+2

About this tool

The modulo operation gives the remainder after division: 17 mod 5 is 2 because 17 is three fives and 2 left over. It drives clock arithmetic, checksums, hashing, repeating patterns and even-or-odd tests. The catch is negative numbers, where programming languages and spreadsheets disagree. This calculator shows the truncated, floored and Euclidean results side by side, with exact arithmetic for very large integers.

How it works

Enter the dividend a and the divisor n. The calculator returns the quotient q and remainder r that satisfy a=nq+ra = nq + r, where the remainder is smaller than the divisor in size.

When both numbers are positive, everyone agrees on the answer. With negative numbers there are three conventions. Truncated division rounds the quotient toward zero, so the remainder takes the sign of a; this is the % operator in C, Java and JavaScript. Floored division rounds the quotient down, so the remainder takes the sign of n; this is Python's % and Excel's MOD. Euclidean division always gives a remainder from 0 up to ∣n∣−1|n| - 1. The headline answer is the Euclidean remainder, the usual one in mathematics; for a positive divisor it matches Python and Excel.

a=nq+r,q=⌊an⌋,r=a−n⌊an⌋a=nq+r,\qquad q=\left\lfloor\frac{a}{n}\right\rfloor,\qquad r=a-n\left\lfloor\frac{a}{n}\right\rfloor

Example

For −7 mod 3-7 \bmod 3: truncated division gives q=−2q = -2 and r=−1r = -1, because −7=3×(−2)+(−1)-7 = 3 \times (-2) + (-1); JavaScript's -7 % 3 returns -1. Floored and Euclidean division give q=−3q = -3 and r=2r = 2, because −7=3×(−3)+2-7 = 3 \times (-3) + 2; Python and Excel return 2.

With a negative divisor, 7 mod −37 \bmod -3 is 1 when truncated, −2 when floored and 1 in the Euclidean convention.

Assumptions and limits

Integers of up to 100 digits are handled exactly. Terminating decimals are scaled to whole numbers first, so 5.5 mod 2 is exactly 1.5. The divisor cannot be zero. Some languages differ in other details, such as how they treat floating-point values or very large numbers, so check the documentation for production code.

Frequently asked questions

What does mod mean in math?

a mod n is the remainder when a is divided by n. 17 mod 5=217 \bmod 5 = 2 and 20 mod 5=020 \bmod 5 = 0. Two numbers are congruent modulo n when they leave the same remainder.

What is -7 mod 3?

It depends on the convention: 2 in mathematics, Python and Excel (floored and Euclidean), and −1 with the % operator of C, Java and JavaScript (truncated). Both satisfy a=nq+ra = nq + r with different quotients.

Why does JavaScript give a negative remainder?

JavaScript, C, C++, Java and C# truncate the quotient toward zero, so the remainder keeps the sign of the dividend. To get a non-negative result for a positive n, use ((a % n) + n) % n.

Is modulo the same as remainder?

For positive numbers, yes. With negative numbers, "remainder" usually means the truncated result and "modulo" the floored or Euclidean one, but usage varies between languages.

How is modulo used in everyday life?

Clock time is arithmetic modulo 12 or 24: 9 hours after 8 o'clock is 17 mod 12=517 \bmod 12 = 5 o'clock. Weekdays work modulo 7, and check digits on barcodes and IBANs use modulo 10 and 97.

Link with your values

Add these parameters to this page’s address to open it with the values filled in, for example to share a calculation or to link to it from an AI assistant. The result is calculated in the browser: the page HTML shows an example for the default values, and the tool sets the data-url-params attribute once the link’s values are applied.

ParameterValueIf omitted
dividendDividend (a)decimal number, kept exactly as writtenrequired
divisorDivisor (n)decimal number, kept exactly as writtenrequired

Examples

  • ?dividend=-17&divisor=5
  • ?dividend=7.5&divisor=2

Write decimals with a point. Encode a space as %20, + as %2B, # as %23 and & as %26.

Result elements: [data-result-block="modulo_calculator"]