Toolstia / Modulo calculator
Modulo calculator
The modulo operation answers a simple question: when you divide one whole number by another, what is left over? 17 divided by 5 goes 3 times with 2 left, so 17 mod 5 is 2. It sits behind clock arithmetic, checking whether a number is even, cycling through a list, repeating patterns, check digits and a good deal of cryptography.
Enter a dividend and a divisor and this calculator shows the mathematical result, the quotient and the full division sentence. For negative numbers it also shows the results of the two common programming conventions side by side, because the same expression gives different answers in different languages.
Numbers are handled as exact whole-number values of any size, not as ordinary floating-point numbers, so very large inputs are not rounded. The sums are done in your browser and nothing is uploaded.
Enter two whole numbers. Negative numbers are allowed.
For positive numbers all three results agree. They differ when a number is negative, which is why the same expression can give different answers in different programming languages.
How it works
- Enter the dividend and the divisor.
- Read a mod n, the quotient and the division.
- Compare the conventions if a number is negative.
The basic idea
17 mod 5 is 2, because 17 = 5 × 3 + 2. The result is how much is left after taking out as many whole copies of the divisor as fit.
Negative numbers
Languages disagree. JavaScript, C and Java give -2 for -17 % 5 (the sign follows the dividend). Python gives 3 (the sign follows the divisor). Mathematicians usually want a result from 0 up to n − 1, which is 3 here.
Uses
Modulo tells you whether a number is even or odd, wraps clock hours around (15 mod 12 = 3), cycles through a list, and underlies checksums and encryption.
Exact big numbers
Whole numbers of any size are handled exactly, not rounded.
A worked example with a negative number
Enter -17 and 5. The quotient rounded toward zero is -3, and -17 = 5 x -3 + -2, so the remainder used by JavaScript, C and Java is -2. Python floors the quotient to -4, so -17 = 5 x -4 + 3 and its modulo is 3. The mathematical, never-negative result is also 3.
The page shows all three on separate lines: the mathematical modulo (the main result), the truncated remainder, and the floored modulo. For positive numbers the three always agree. The equation line shown always uses the truncated remainder and the quotient rounded toward zero, so read it together with the table.
Negative divisors
With a negative divisor the results can differ. For 17 and -5 the truncated remainder is 2 and the quotient is -3, because 17 = -5 x -3 + 2. The floored version, as in Python, is -3, and the mathematical result is 2, which uses the size of the divisor. For -17 and -5 the mathematical result is 3, while the truncated remainder and the Python-style result are both -2.
The main result line always gives a value from 0 up to one less than the size of the divisor, so it is the right choice when you are doing maths and not matching a particular language.
Everyday uses with real numbers
Clock time: 15 mod 12 is 3, so 15:00 is 3 o'clock. Weekdays: seven days make a week, and 100 mod 7 is 2, so a day 100 days after a Tuesday is a Thursday. Even or odd: n mod 2 is 0 for even numbers and 1 for odd ones. Cycling: to put item number 25 in a ring of 8 positions numbered from 0, use 25 mod 8, which is 1.
Modulo also shows up in divisibility rules. A number is divisible by n exactly when its remainder is 0. 123456789012345678901234567890 mod 97 is 52, so that number is not divisible by 97, which is a check no ordinary calculator can make exactly.
Rules that make modulo useful
Remainders respect addition and multiplication: (a + b) mod n equals ((a mod n) + (b mod n)) mod n, and the same holds for multiplication. That lets you work with small numbers in the middle of a long calculation. It is the idea behind casting out nines and behind many check-digit schemes.
This tool computes a single remainder. It does not do modular exponentiation, modular inverses or the extended Euclidean algorithm.
Input rules and limits
Both boxes need whole numbers. Commas and spaces are removed, so 1,000,000 is accepted, and a leading plus or minus sign is allowed. Decimals are not accepted and show a prompt to enter whole numbers. A divisor of zero gives a clear error, since division by zero has no answer.
A common slip is using a calculator or spreadsheet that returns a floating-point value for large integers. In ordinary JavaScript, numbers above 9,007,199,254,740,991 lose exactness, which is why this page uses exact integers.
When to use something else
To find the greatest common divisor or least common multiple of two numbers, use a GCD and LCM calculator, which uses the same remainder idea repeatedly. To check whether a number is prime or to see its factors, use a prime factorisation tool. For decimals, remainders are rarely meaningful and a division calculator is a better fit.
Questions
What is 0 mod n?
It is 0 for any non-zero n.
Can the divisor be negative?
Yes. The conventions are shown separately so you can pick the one your language uses.
Is anything uploaded?
No. Your numbers stays in your browser and is never sent anywhere.
What is the difference between modulo and remainder?
For positive numbers there is none. They differ for negative numbers. A remainder follows the sign of the dividend in languages like C and JavaScript, while a mathematical modulo is never negative.
How do I check if a number is even using modulo?
Compute n mod 2. If the answer is 0 the number is even, and if it is 1 it is odd. This works for negative numbers too in the main result line.
Why does the answer to 5 mod 17 equal 5?
Because 17 does not fit into 5 even once, so the whole of 5 is left over. Any number smaller than the divisor, and not negative, is its own modulo.
Can I enter very large numbers?
Yes. The tool uses exact integers, so numbers with dozens or hundreds of digits are processed without rounding.