Modulo Operator and Parity
From the csc241\ curriculum
TL;DR
The modulo operator (%) gives you the remainder after division, which is super useful for checking if a number is even or odd (its parity). If a number % 2 is 0, it's even; otherwise, it's odd. Understanding this helps you write code that responds differently based on a number's divisibility.
1. The Mental Model
Think of modulo like sharing cookies. If you have 7 cookies and want to share them equally among 2 friends, each friend gets 3 cookies, and you're left with 1. That "1 left over" is what the modulo operator gives you.
2. The Core Material
The modulo operator, often written as % in programming languages like Python, C, Java, etc., returns the remainder of a division. It's an integer operation, meaning it works with whole numbers.
For example:
* 10 % 3 is 1 (10 divided by 3 is 3 with a remainder of 1).
* 14 % 5 is 4 (14 divided by 5 is 2 with a remainder of 4).
* 8 % 2 is 0 (8 divided by 2 is 4 with a remainder of 0).
Parity: Even or Odd

Photo by Pavel Danilyuk on Pexels
Parity refers to whether a number is even or odd. This is one of the most common uses of the modulo operator.
- An even number is any integer that can be divided by 2 with no remainder.
- An odd number is any integer that leaves a remainder of 1 when divided by 2.
Using the modulo operator, you can check parity like this:
* If number % 2 == 0, the number is even.
* If number % 2 == 1, the number is odd.
This works for both positive and negative integers in many languages, though behavior with negative numbers can sometimes vary slightly depending on the language's implementation (e.g., Python's % always returns a result with the same sign as the divisor, while C/Java's % takes the sign of the dividend). For checking parity with positive numbers, it's straightforward.
# Python example
num1 = 10
num2 = 7
num3 = -4
print(f"{num1} % 2 is {num1 % 2}") # Output: 10 % 2 is 0
print(f"{num2} % 2 is {num2 % 2}") # Output: 7 % 2 is 1
print(f"{num3} % 2 is {num3 % 2}") # Output: -4 % 2 is 0 (Python specific behavior)
# Checking parity
if num1 % 2 == 0:
print(f"{num1} is even")
else:
print(f"{num1} is odd")
if num2 % 2 == 0:
print(f"{num2} is even")
else:
print(f"{num2} is odd")
Here's how you can think about the decision process for checking parity:
graph TD
A["Start"] --> B{"Is number an integer?"}
B -- Yes --> C{"Calculate remainder = number % 2"}
B -- No --> D["Error: Not an integer"]
C --> E{"Is remainder == 0?"}
E -- Yes --> F["Number is Even"]
E -- No --> G["Number is Odd"]
F --> H["End"]
G --> H
D --> H
Other Uses of Modulo

Photo by Borys Zaitsev on Pexels
Beyond parity, modulo is useful for:
* Wrapping around: Like indexing a circular array (index = (index + 1) % array_length).
* Digit extraction: number % 10 gives the last digit of a number.
* Time calculations: Converting total minutes into hours and remaining minutes (minutes % 60).
3. Worked Example
Let's say you're building a simple program that needs to tell you whether a number entered by a user is even or odd.
# Ask the user for a number
user_input = input("Enter an integer: ")
# Convert the input string to an integer
try:
number = int(user_input)
# Check the parity using the modulo operator
if number % 2 == 0:
result = "even"
else:
result = "odd"
# Print the result
print(f"The number {number} is {result}.")
except ValueError:
print("That's not a valid integer! Please try again.")
If the user enters 42:
1. user_input becomes "42".
2. number becomes 42.
3. 42 % 2 is 0.
4. The if condition 0 == 0 is True.
5. result becomes "even".
6. The program prints: "The number 42 is even."
If the user enters 17:
1. user_input becomes "17".
2. number becomes 17.
3. 17 % 2 is 1.
4. The if condition 1 == 0 is False.
5. The else block runs, and result becomes "odd".
6. The program prints: "The number 17 is odd."
4. Key Takeaways
- The modulo operator (
%) gives you the remainder of a division. - You can determine if a number is even if
number % 2 == 0. - You can determine if a number is odd if
number % 2 == 1. - Modulo is crucial for tasks like cycling through options, extracting digits, and time conversions.
- Always remember that the result of
N % Dwill be between0andD-1(for positiveD). - The sign of the modulo result for negative numbers can vary between programming languages; generally, it's best to handle positive inputs for simple parity checks or be aware of language specifics.
Common Mistakes to Avoid:
- Forgetting that % is the modulo operator, not just a percentage sign.
- Confusing modulo with integer division (// in Python, / in C/Java for integers), which gives the quotient.
- Not handling non-integer inputs when reading from a user, leading to type errors.
- Assuming number % 2 will always be 1 for odd numbers with negative inputs across all languages.
- Using == for assignment = or vice-versa in conditional checks.
5. Now Try It
Write a Python program that takes a number N from the user. Then, iterate from 1 to N (inclusive), and for each number in that range, print whether it's "Even" or "Odd". If the user enters 5, your output should list "1 is Odd", "2 is Even", ..., "5 is Odd". Your program should also handle non-integer input gracefully by printing an error message.
Frequently asked about Modulo Operator and Parity
More from csc241\
Get the full csc241\ curriculum
Clone the complete plan to your dashboard for unlimited AI-generated notes, practice quizzes, and a personalised revision schedule.
Save this course free