mod function calculator

Mod Function Calculator

Compute the modulo of two numbers instantly. This tool supports both mathematical modulo (always non-negative) and JavaScript remainder behavior.

Tip: Divisor cannot be zero. Press Enter or click Calculate.

Quick examples:
Enter values above to see your result.

What is the mod function?

The mod function (short for modulo) returns the remainder after dividing one number by another. If you write a mod n, you are asking: “What is left over when a is divided by n?”

For example, 29 mod 5 = 4 because 29 = (5 × 5) + 4. The remainder is 4.

Why modulo is useful in real life

Modulo is one of the most practical operations in math and programming. You can use it in:

  • Time calculations: 26 hours after 3:00 is 5:00 (because 26 mod 24 = 2).
  • Calendars: Find the day of week offsets with mod 7 arithmetic.
  • Even/odd checks: If n mod 2 = 0, n is even.
  • Data structures: Hash tables often use modulo to map values into bucket ranges.
  • Cryptography: Many encryption systems rely on modular arithmetic.

How to calculate a mod n

Standard formula

You can compute modulo with:

a mod n = a - n × floor(a / n) (for positive n).

This guarantees the result is between 0 and n-1.

Example

Compute 43 mod 6:

  • 43 / 6 = 7.166...
  • floor(43 / 6) = 7
  • 43 - (6 × 7) = 1

So, 43 mod 6 = 1.

Negative numbers: modulo vs remainder

This is where many calculators and languages differ.

Mathematical modulo

Usually defined to stay non-negative (for positive divisor). Example:

-29 mod 5 = 1

Because -29 = (5 × -6) + 1.

Programming remainder

Some languages (including JavaScript’s %) return a remainder with the sign of the dividend:

-29 % 5 = -4

That is mathematically consistent for remainder, but different from the non-negative modulo many people expect.

Use this calculator correctly

  • Choose Mathematical modulo if you want an always non-negative result.
  • Choose JavaScript remainder if you want behavior that matches JavaScript code exactly.
  • Avoid divisor zero; modulo by zero is undefined.

Common quick checks

Is a number even?

If n mod 2 = 0, then n is even.

Last digit of a number

n mod 10 gives the last digit in base-10.

Cyclic indexing

If you have 7 items and keep incrementing a pointer, use index mod 7 to wrap around automatically.

Final thought

The mod function is a small concept with massive practical value. Whether you are solving math problems, writing code, or building systems that cycle through values, modulo helps you reason cleanly about repetition, boundaries, and structure.

🔗 Related Calculators