Modulo Calculator
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
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
Related Calculators
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
Visual Example
Common Applications
Checking Even/Odd
n % 2 == 1 → Odd
Clock Arithmetic
Clock "wraps around"
Array Indexing
Cycles through elements
Cryptography
modular exponentiation
Properties of Modulo
Programming Languages
| Language | Operator | Example |
|---|---|---|
| JavaScript, Python, Java, C | % | 17 % 5 |
| Python (true modulo) | % | -17 % 5 = 3 |
| Excel | MOD() | MOD(17, 5) |
| Haskell | mod | 17 `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.