Modulo Calculator

Examples:

What is Modulo?

The modulo operation (mod) finds the remainder after division of one number by another. In programming, it's often represented as % (e.g., 17 % 5 = 2).

Mathematical Definition

a mod b = a - b × floor(a/b)

The result always has the same sign as the divisor (in standard math convention).

Common Uses

  • Check if a number is even (n % 2 == 0)
  • Wrap around values (clock arithmetic)
  • Hash functions and array indexing
  • Cryptographic algorithms

Properties

  • 0 ≤ (a mod b) < |b|
  • (a + b) mod n = ((a mod n) + (b mod n)) mod n
  • (a × b) mod n = ((a mod n) × (b mod n)) mod n

Quick Reference

17 mod 5 = 2
100 mod 7 = 2
25 mod 4 = 1
12 mod 3 = 0

Understanding Modulo Operations

What is Modulo?

The modulo operation (often written as "mod" or "%") calculates the remainder after dividing one number by another. It's one of the fundamental operations in mathematics and is extensively used in computer science and programming.

Basic Formula

a mod b = a - b × floor(a/b)
Or simply: "What's left over after dividing?"

Visual Example

17 mod 5 = ?
3 complete groups of 5
2 remaining
17 = 5 × 3 + 2

Common Applications

Checking Even/Odd

n % 2 == 0 → Even
n % 2 == 1 → Odd

Clock Arithmetic

(14:00 + 12 hours) % 24 = 2:00
Clock "wraps around"

Array Indexing

index % array.length
Cycles through elements

Cryptography

RSA encryption uses
modular exponentiation

Properties of Modulo

Addition:
(a + b) mod n = ((a mod n) + (b mod n)) mod n
Subtraction:
(a - b) mod n = ((a mod n) - (b mod n)) mod n
Multiplication:
(a × b) mod n = ((a mod n) × (b mod n)) mod n
Identity:
a mod a = 0 (any number mod itself is 0)

Programming Languages

LanguageOperatorExample
JavaScript, Python, Java, C%17 % 5
Python (true modulo)%-17 % 5 = 3
ExcelMOD()MOD(17, 5)
Haskellmod17 `mod` 5

Note on Negative Numbers

Different programming languages handle negative numbers differently. In Python, -17 % 5 = 3 (mathematical definition), while in JavaScript/Java, -17 % 5 = -2 (remainder has sign of dividend). Be aware of this when working across languages.

Frequently Asked Questions

What is the difference between modulo and remainder?

For positive numbers, modulo and remainder are the same. For negative numbers, they differ: remainder keeps the sign of the dividend (-17 % 5 = -2 in most languages), while true modulo is always non-negative (-17 mod 5 = 3). Python uses true modulo, while JavaScript/Java use remainder.

How is modulo used in programming?

Modulo is used to: check if a number is even (n % 2 == 0), cycle through arrays (index % array.length), implement circular buffers, wrap around clocks (hour % 12), create hash functions, and implement cryptographic algorithms like RSA.

Why does any number mod 1 always equal 0?

Because every integer is perfectly divisible by 1 with no remainder. When you divide any number by 1, you get the number itself with remainder 0. For example: 5 mod 1 = 0, 100 mod 1 = 0, -7 mod 1 = 0.

How does clock arithmetic use modulo?

A 12-hour clock uses mod 12 arithmetic. 14:00 becomes 14 mod 12 = 2 (2 PM). Adding 5 hours to 10:00: (10 + 5) mod 12 = 3. This 'wrap around' behavior is the essence of modular arithmetic and appears in many cyclic systems.