Skip to main content
Data / Number / Computer Science Tools

Modulo Calculator

Calculate the remainder of a division - clearly explains how negative operands are handled, since modulo behavior differs across languages.

Enter two numbers - this tool calculates the remainder and explains the result, especially for negative operands where behavior varies by language.

mod

When both operands are positive, these are identical. With a negative dividend, JavaScript's % (and most C-family languages) returns a result with the same sign as the dividend - the "true" mathematical modulo (as used in Python) always returns a result with the same sign as the divisor. Both are shown so you can see the difference clearly.

JavaScript's % operator (which this tool uses) returns a result with the same sign as the dividend - some languages (like Python) instead return a result with the same sign as the divisor. Both are documented below.

Modulo vs. Remainder - Why Negatives Matter

For positive numbers, "modulo" and "remainder" always agree. For negative operands, they can differ: JavaScript, C, and Java's % operator gives a result with the same sign as the dividend (true "remainder" semantics), while Python's % gives a result with the same sign as the divisor (true "modulo" semantics).

How to Use It

  1. Enter a dividend and a divisor.
  2. View the result using JavaScript/remainder semantics, alongside the Python-style modulo result for comparison when they'd differ.

Handling Division by Zero

Division/modulo by zero is mathematically undefined - this tool reports that clearly rather than returning a misleading NaN with no explanation.

Examples

ExpressionJavaScript / C / Java (%)Python (%)
17 mod 522
-17 mod 5-23
17 mod -52-3
10 mod 200

Everyday Uses

  • Even or odd: n % 2 === 0 means even.
  • Wrapping around: the day of the week 10 days after Friday is (5 + 10) % 7.
  • Time: 135 minutes is 2 hours and 135 % 60 = 15 minutes.
  • Alternating rows: style every third row with index % 3 === 0.
  • Hashing and sharding: id % 4 picks one of four servers or buckets.

Getting a Positive Result in JavaScript

To get Python-style results (always matching the divisor's sign), use ((a % n) + n) % n. For example, ((-17 % 5) + 5) % 5 is 3 - useful when wrapping array indexes backwards.

For percentages, use the Percentage Calculator; for bit-level maths, the Bitwise Calculator.

Frequently Asked Questions

The remainder after division. 17 mod 5 = 2, because 17 = 3 x 5 + 2.

JavaScript, C and Java use the remainder with the sign of the dividend; Python uses true modulo with the sign of the divisor. The tool shows both.

Checking even/odd numbers, wrapping values around (clock times, circular lists), alternating row colours and hashing.
Share this tool: