Understanding the Modulo Operation

In programming and mathematics, the modulus operator (%) plays a very important role but often gets overlooked. It’s a simple yet powerful tool once you know how to use it. Let’s dive into what it is, how it works, and where it is commonly used.

What is Modulo?

Modulo (often written as % in programming) is an operation that finds the remainder when one number is divided by another.

In simple words:

a mod b = remainder when a is divided by b
Python

Where:

  • a = dividend (the number you divide)
  • b = divisor (the number you divide by)
  • result = remainder

General modulo formula:

a mod b = a − b×⌊a/b⌋
Python

where ⌊⌋ is the floor (rounding down).

Derivation

Dividend = Divisor * Quotient + Remainder

Remainder = Dividend - Divisor * Quotient

Remainder = Dividend - Divisor * ⌊Dividend/Divisor⌋
Python

Note

Quotient = ⌊Dividend/Divisor⌋
Python

where ⌊⌋ is give whole integer

Example Floor

x⌊x⌋
5.85
-5.8-6
2.12
-2.1-3

Note

  • Rounding down

Example for floor function

10/3=3.33= 3
13/6=2.166= 2
Python

Mod Example

ExpressionCalculationShorthandResult
10 % 3r = 10 – 3*⌊10/3⌋10 ÷ 3 = 3 remainder 1
1
20 % 4r = 20 – 4*⌊20/4⌋20 ÷ 4 = 5 remainder 0
0
15 % 6r = 15 – 6* ⌊15/6⌋15 ÷ 6 = 2 remainder 3
3
5 % 8r = 5-8*⌊5/8⌋5 ÷ 8 = 0 remainder 55

Example 2: Negative Values

ExpressionCalculationResult
10 % 3r = 10 – 3*⌊10/3⌋
r = 10 -3*3
r = 1
1
10 % -3r = 10 – (-3)*⌊10/-3⌋
r = 10 +3*(-4)
r =-2
-2
-10 % 3r = (-10) – 3* ⌊-10/3⌋
r = -10 -3*(-4)
r = 2
2
-10 % -3r = (-10)-(-3)*⌊-10/-3⌋
r = -10+3*3
r =-1
-1

Till now every thinks fine, as expected But

But

CaseExpressionResult in CResult in PythonMath
110 % 3111
210 % -31-2-2
310 % 3-122
4-10 % -3-1-1-1

Why case 2 and case 3 have different result

The Key Reason: Division Semantics

The modulo operation % is tightly connected to how integer division (/ or //) behaves. There are two common types:

In C:

  • The sign of the result follows the numerator (left operand).
  • It performs truncating division.

In Python:

  • The sign of the result follows the denominator (right operand).
  • It performs floor division: a % b == a – (a // b) * b
Type of DivisionRounds TowardExample (-10 / 3)Result
Truncating (C)0 (zero)-10 / 3-3.333-3mod: -10 - (-3×3) = -1% = -1
Flooring (Python)-∞ (down)-10 // 3-4mod: -10 - (-4×3) = 2% = 2
  • C uses truncating division, so % keeps the sign of the dividend (numerator).
  • Python uses floor division, so % keeps the sign of the divisor (denominator).

Python

print(10/-3) #-3.333
print(int(10/-3)) #-3 This is equivalent to C/C++  Truncating
print(10//-3) #-4, Flooring
Python

Why Different Choices?

  • C (and Java, etc.) chose truncation because it’s simpler and faster on hardware.
  • Python chose floor division because it’s mathematically consistent

Conclusion

The modulus operator is small but mighty. Whether you’re checking if a number is even, cycling through options, or managing boundaries, % can make your code cleaner and faster.
Once you start using it, you’ll find it everywhere in both basic and advanced programming problems!

Resource

1 thought on “Understanding the Modulo Operation”

Leave a Comment