Modulo Calculator

Find a mod n with floored, truncated and Euclidean definitions, including negative numbers. Every version satisfies a = n × q + r, with a remainder r smaller than |n|. Example: with dividend a -7, divisor n 3, the result is -7 mod 3 (floored, Python style) = 2.

Last updated · By · Runs in your browser, nothing is uploaded

Result

Step-by-Step Solution

Enter your values above to see the full working.

How do you use the Modulo Calculator?

Enter the dividend a and the divisor n, both whole numbers, positive or negative. The calculator returns the remainder under the three common definitions: floored (the Python style), truncated (the C, Java and JavaScript % operator) and Euclidean (always zero or positive). It also shows the floored quotient.

For positive numbers all three agree, so the differences only matter when one input is negative. The solution box explains each result step by step so you can see why they diverge.

What formula does the Modulo Calculator use?

Every version satisfies a = n × q + r, with a remainder r smaller than |n|. They differ in how the quotient q is rounded. Floored modulo uses q = ⌊a ÷ n⌋, so the result takes the sign of the divisor. Truncated remainder rounds q toward zero, so the result takes the sign of the dividend. The Euclidean remainder is defined so that r is always in the range 0 to |n| − 1.

Mathematicians usually mean the Euclidean version when they write a mod n or a ≡ r (mod n). Programmers should check which one their language uses, because the same expression can give different answers in different languages.

Worked example: −7 mod 3

The floored quotient is ⌊−7 ÷ 3⌋ = ⌊−2.33⌋ = −3, so the floored modulo is −7 − 3 × (−3) = 2. The truncated quotient is −2, so the truncated remainder is −7 − 3 × (−2) = −1. The Euclidean remainder is 2. So in Python −7 % 3 is 2 but in JavaScript −7 % 3 is −1, a classic source of bugs when wrapping array indexes or computing clock times.

Where modulo is used

Modulo describes anything that repeats in a cycle. Clock arithmetic works modulo 12 or 24, days of the week work modulo 7, and even and odd numbers are the remainders modulo 2. In programming it wraps indexes around circular buffers, distributes keys across hash table buckets, and checks divisibility. In cryptography, modular arithmetic underpins RSA and Diffie-Hellman.

A frequent real-world case is converting seconds into minutes: 135 seconds is 135 mod 60 = 15 seconds after 2 full minutes, found by the floored quotient.

What are the most common mistakes with the Modulo Calculator?

  • Assuming the result is never negative. It can be in languages that use truncated remainder.
  • Dividing by zero. The divisor cannot be 0.
  • Mixing up the quotient and remainder, which are different outputs of the same division.
  • Applying integer modulo to decimals, which is defined differently in many languages.

What Is the Tested Example for the Default Values?

This example is computed by the same code that powers the calculator above, so you can compare it with your own numbers or a textbook answer.

Input: Dividend a-7
Input: Divisor n3
-7 mod 3 (floored, Python style)2
Truncated remainder (C, Java, JavaScript %)-1
Euclidean remainder (always ≥ 0)2
Floored quotient-3

Where Can You Verify These Formulas?

The definitions and formulas on this page follow standard references. Check them against these sources:

Built and maintained by S. Dilshan. Found a mistake? Email [email protected] with the page address.

Frequently Asked Questions

What does a mod n mean?

It is the remainder when a is divided by n. For example, 17 mod 5 is 2 because 17 = 3 × 5 + 2.

Why does −7 mod 3 give different answers in different languages?

Languages round the quotient differently. Python floors it, giving 2, while C, Java and JavaScript truncate toward zero, giving −1.

What is modular arithmetic used for?

Clocks, calendars, hashing, error-detecting codes and public-key cryptography all rely on it.