Modulo Calculator

Free modulo calculator: find a mod n, the remainder and quotient with a step-by-step a = n x q + r breakdown. Handles negative numbers, decimals and modular congruence.

https://hexacalculator.com/calculators/mathematics/arithmetic/modulo-calculator

Mathematics

Arithmetic

Modulo Calculator

Free modulo calculator: find a mod n, the remainder and quotient with a step-by-step a = n x q + r breakdown. Handles negative numbers, decimals and modular congruence.

Modulo Calculator

Modulo calculator

17 mod 5 = 2. 17 divided by 5 is 3 with remainder 2, so 17 = 5 × 3 + 2.

Quotient floor(a / n)

Step-by-step division

Every modulo is a single division: a = n x quotient + remainder, and the last row rebuilds a

Step

Value

Dividend a17
Divisor n5
Quotient floor(a / n)3
n x quotient15
Remainder r = a - n x quotient2
Check n x quotient + r17

See the remainder wrap around

Loading calculator…

The modulo operation answers the question of what is left over when you divide one number aa by another number nn. The remainder is called the "modulo" and is represented as amodna \bmod n. For example: 17mod5=217 \bmod 5 = 2. If you split 17 into groups of five, you get three full groups with two remaining.

This calculator will give you the remainder, quotient and full breakdown of a = n × q + r when you input the dividend and divisor above. It supports negative numbers and decimals, and it shows if the result of the modulo operation is not the same as "remainder" used by programmers. Switch to congruence mode to check if two numbers have the same remainder.

What is a modulo operation?

Let's start with a normal division with remainder. When you divide aa (dividend) by nn (divisor, also called modulus), you get an integer quotient qq and a remainder rr, such that a=nq+ra = nq + r. In modulo operation we keep only the remainder and discard the quotient.

In a strict definition, the rounding function is used, i.e., rounded to the nearest integer.

The formula for modulo operation is amodn=anana \bmod n = a - n\left\lfloor \dfrac{a}{n} \right\rfloor.

If nn is a positive number, the result will always be within the range of 0r<n0 \le r < n. So the modulo operation never exceeds the divisor. To easily check this, make sure that amodna \bmod n is always less than nn itself.

When the divisor is larger than the dividend, it's a very simple special case. Since no quotient can be calculated, the remainder is equal to the dividend itself. This is because 2 and 10 both occur zero times in their respective dividends, so 1mod2=11 \bmod 2 = 1 and 5mod10=55 \bmod 10 = 5.

How to calculate modular arithmetic manually.

This method is based on long division which you probably already know how to do. You can turn a division problem into a modulo operation with four steps.

Let's take 23mod723 \bmod 7 as an example.

  1. I divide: 23 ÷ 7 = 3.285...

  2. I round down to get a quotient of 3.

  3. I multiply back. So, 7 times 3 is equal to 21.

  4. You subtract and determine the remainder. So it is 23mod7=223 \bmod 7 = 2.

The instructions on this page will perform these steps precisely for each value entered. The last line recalculates aa based on n×q+rn \times q + r, and verifies the result.

Some example calculations:

Below are some of the common questions or issues that may be addressed by this calculator.

Expression

Quotient

Remainder

Why

27 mod 6

4

3

27 = 6 x 4 + 3

17 mod 5

3

2

17 = 5 x 3 + 2

29 mod 6

4

5

29 = 6 x 4 + 5

250 mod 24

10

10

250 = 24 x 10 + 10

100 mod 10

10

0

10 divides 100 exactly

3 mod 8

0

3

8 is larger than 3

Modulo operations with negative numbers:

Negative numbers are an area where the modulo calculation tool shows its strengths because two apparently plausible conventions do not agree. The rounding definition given above still applies. For example, if we consider 7mod3-7 \bmod 3, the quotient 7/3\lfloor -7/3 \rfloor is rounded to 3-3, which gives a remainder of 73(3)=2-7 - 3(-3) = 2. According to the mathematical definition, the result would be 7mod3=2-7 \bmod 3 = 2, a positive number.

Most programming languages do not use this result. In C, Java and JavaScript the operator %\% truncates the quotient towards zero, so that 7%3=1-7 \% 3 = -1. Both methods are consistent within their respective rules but they answer slightly different questions. A useful rule of thumb is that modulo with rounding takes the sign of the divisor while truncated remainder takes the sign of the dividend.

This calculator primarily shows the result of the mathematical (rounded) modulo operation. If the results differ due to a minus sign, for each case the rounded remainder is shown next to it so that you can use the result accepted in your language.

Convention

Languages

-7 mod 3

7 mod -3

Floored (sign of divisor)

Python, Ruby

2

-2

Truncated (sign of dividend)

C, Java, JavaScript

-1

1

Modular congruence:

Two numbers aa and bb are congruent modulo nn if they have the same remainder. This is denoted by ab(modn)a \equiv b \pmod{n}. Another equivalent condition is that the difference aba - b is a multiple of nn.

For example: 1712(mod5)17 \equiv 12 \pmod 5. Both remainders are 2 and the difference 1712=517 - 12 = 5 is a multiple of 5. The same applies to: 921(mod6)9 \equiv 21 \pmod 6. 219=1221 - 9 = 12 is a multiple of 6.

In the congruence mode this is checked and all numbers that have the same remainder are listed as a set called a congruence class. Any number obtained by adding or subtracting the modulus belongs to this set.

Rules for modulo arithmetic.

Congruence relations work well with addition, subtraction and multiplication. That's why modular arithmetic is so useful. For positive moduli we have:

(a+b)modn=((amodn)+(bmodn))modn(a + b) \bmod n = \big((a \bmod n) + (b \bmod n)\big) \bmod n

(a×b)modn=((amodn)×(bmodn))modn(a \times b) \bmod n = \big((a \bmod n) \times (b \bmod n)\big) \bmod n

This means that you can apply the modulo operation separately to each part and then combine them. This keeps the numbers small. This is the secret of how computers can compute extremely large numbers in cryptography without having to store them in their full form.

Examples of applications for modular arithmetic.

A very common example is the clock. The 12-hour format is based on modulo-12 arithmetic. Four hours after 11 o'clock it's (11+4)mod12=3(11 + 4) \bmod 12 = 3. Also, days of the week work in a similar way with the modulo-7 system.

Besides timing, the modulo operation is one of the most commonly used operations in programming. By calculating n mod 2, you can determine whether a number is even or odd. With i mod length, an index can be kept within the bounds of an array. Hash tables, round-robin schedulers, checksums for credit cards and ISBNs, and public-key cryptography all rely on this same operation of cyclical repetition.

About division by zero:

The divisor nn can be positive, negative or a decimal number but cannot be zero. As division by zero is undefined, so is amod0a \bmod 0. If you enter zero as the divisor, this calculator will not give an instant result but instead explicitly inform you about it to avoid any confusion.

Frequently asked questions

What is a modulo calculator?

A modulo calculator computes the remainder of a division. It calculates a mod n, which is what remains after dividing the dividend a by the divisor n. For example: 17 mod 5 = 2. When 17 is divided by 5, the quotient is 3 and the remainder is 2. This tool shows both the quotient and the decomposition a = n × q + r.

How do you calculate a modulo operation step by step?

One divides a by n, rounds down the quotient, multiplies this rounded-down quotient with n and then subtracts the result from a. The remaining result is the result of the modulo operation. In the example 23 mod 7, dividing 23 by 7 gives approximately 3.28, which becomes 3 after rounding down. Multiplying 7 by 3 gives 21, and subtracting 21 from 23 gives 2. Therefore 23 mod 7 = 2.

What is the formula for modulo operation?

The modulo operation is defined as a mod n = a minus the product of n and the rounded down value of a divided by n. Since the rounding function rounds to the next lowest integer, for positive numbers the remainder will be between 0 and n - 1. This formula works correctly even with negative numbers.

What is the difference between modulo operation and remainder?

For positive numbers the results agree, but they can differ when negative numbers are involved. The mathematical modulo operation used by Python and Ruby takes into account the sign of the divisor and is always non-negative when the divisor is positive. The truncated remainder calculation used in C, Java, and JavaScript takes into account the sign of the dividend. Thus −7 mod 3 is 2 by the former convention while it is −1 by the latter convention.

What happens when the dividend is smaller than the divisor?

The remainder is the number left over after division. If n is larger than a, then there are no multiples of n that fit into a, so nothing gets divided out and everything remains. For example, 3 mod 8 = 3, 5 mod 10 = 5. This statement holds true if both numbers are positive.

What does congruence modulo n mean?

When the remainder of dividing two numbers by n is equal, then those numbers are said to be congruent modulo n and written as a ≡ b (mod n). An equivalent statement is that their difference is a multiple of n. For example, 17 and 5 are congruent modulo 12 because both have the same remainder, 5, and the difference between 17 and 5 is 12, which is a multiple of n.

How is modulo operation used in real life?

The modulo operation is useful for all sorts of things that are cyclical or repetitive. Clocks show time between 1 and 12, and calendars cycle through the seven days of a week. In programming it's used to determine if a number is even or odd, to keep array indices within bounds, to support hash tables, and forms the core of checksums and public-key cryptography.

Related calculators

Disclaimer: This calculator is provided for general informational and educational purposes only. Our calculators are under active development, and results may be inaccurate, incomplete, or unsuitable for your situation. Always verify the figures independently and seek advice from a qualified professional before relying on them. We make no warranties and accept no liability for any loss or decision arising from use of this tool.

References

  1. Wikipedia: Modulo

    The modulo operation across mathematics and programming, including the floored and truncated conventions.

  2. Wikipedia: Modular arithmetic

    Congruence, residue classes and the arithmetic rules that make modulo useful in number theory.