Social media growth services from EthicalSMM

Modulo Calculator

Find a mod b, the remainder and the quotient, and see how negative numbers differ between Python and Excel and C, Java or JavaScript.

Was this tool helpful?

Thanks for the feedback!

This calculator finds a mod b: the remainder left when one number is divided by another, along with the quotient. It shows both conventions side by side, the floored modulo used by Python and Excel and the truncated remainder used by C, Java and JavaScript, so you can see exactly why the same sum gives different answers with negative numbers. It suits programmers, students learning modular arithmetic and anyone working out clock or calendar sums.

The fields

  • Dividend (a): the number being divided, such as 17.
  • Divisor (b): the number you divide by, such as 5.

Type plain digits with an optional minus sign and decimal point. Commas are not accepted here, so enter 1000 rather than 1,000, otherwise you see "Enter two numbers, the dividend and the divisor." Spaces at either end are ignored.

Floored modulo and truncated remainder

Every division can be written as a = b × q + r. The question is how q is rounded when the result is not whole.

  • The floored version rounds q down, towards minus infinity. The remainder then has the same sign as the divisor. Python's % operator and Excel's MOD() work this way.
  • The truncated version cuts q towards zero. The remainder then has the same sign as the dividend. The % operator in C, C++, Java and JavaScript works this way.

For two positive numbers the two agree. The tool's big result is the floored modulo, and the rows below list the floored modulo, the truncated remainder, both quotients and a check line that rebuilds the dividend. When either number is negative a note explains that the two conventions differ.

Worked examples

InputsFloored (Python, Excel)Truncated (C, Java, JS)Quotients (floored / truncated)
17 mod 5223 / 3
-17 mod 53-2-4 / -3
17 mod -5-32-4 / -3
-17 mod -5-2-23 / 3
5.5 mod 21.51.52 / 2

Take -17 mod 5. Floored division gives q = -4, because -3.4 rounded down is -4, and the check line reads 5 × -4 + 3 = -17, so the modulo is 3. Truncated division gives q = -3 and a remainder of -2. If code ported from Python to JavaScript suddenly returns negative indices, this difference is usually the cause.

Some everyday uses:

  • Days of the week: 100 mod 7 is 2, so 100 days after a Monday is a Wednesday.
  • Clock time: 45 mod 24 is 21, so 45 hours after midnight is 9 pm.
  • Even or odd: a number is even when n mod 2 is 0.
  • Divisibility: 2026 mod 4 is 2, so 2026 is not divisible by 4 and is not a leap year. The full leap-year rule also treats century years separately.

Very large numbers and decimals

When both inputs are whole numbers and either has more than 15 digits, the tool switches to exact integer arithmetic so no digits are lost. In that mode it shows the floored modulo, the truncated remainder and the truncated quotient. For example, 123456789012345678901 mod 97 is 31, with a quotient of 1272750402189130710. Mod 97 on long numbers is the kind of sum used to check IBAN bank account numbers.

Decimals are handled in normal floating-point maths, which cannot store most decimal fractions exactly. That is why 0.3 mod 0.1 shows 0.1 instead of 0: the computer holds 0.3 as slightly less than three lots of 0.1. Python gives the same kind of result. For money, convert to paise first and use whole numbers.

A divisor of 0 gives "You cannot divide by zero." If you only want the plain decimal result of a division, the fraction to decimal calculator is quicker. For powers, including the large ones used in modular arithmetic, see the exponent calculator. Everything runs in your browser.

Frequently asked questions

What is 17 mod 5?
17 mod 5 is 2, because 5 goes into 17 three times (15) with 2 left over. Every common convention agrees when both numbers are positive.
Why does -17 mod 5 give 3 in Python but -2 in JavaScript?
Python rounds the quotient down to -4, so the remainder is 3 and takes the sign of the divisor. JavaScript cuts the quotient towards zero to -3, so the remainder is -2 and takes the sign of the dividend.
Does Excel MOD work like Python or like Java?
Excel MOD works like Python: the result takes the sign of the divisor. MOD(-17, 5) returns 3, the floored modulo this calculator shows in large type.
What is the difference between modulo and remainder?
For positive numbers they are the same. With negatives, "modulo" usually means the floored result that follows the divisor's sign, and "remainder" the truncated result that follows the dividend's sign.
Can I use modulo with decimal numbers?
Yes, 5.5 mod 2 gives 1.5. Some decimals cannot be stored exactly in binary, so a sum like 0.3 mod 0.1 shows 0.1 instead of 0, the same as in most programming languages.
How do I find what day it will be in 100 days?
Work out 100 mod 7, which is 2, and count two days forward from today. From a Monday, 100 days later is a Wednesday.

Related guides

Related tools

People also used

Embed this tool on your site

Paste it into any blog or website. Free, with a credit link.