Lesson 34: Using % Remainder Arithmetic Operator
Learn how % returns the remainder after division, and how to use it for checks like even/odd logic.
Progress indicator
Lesson 34 of 57
Learning Objectives
- Understand what the remainder operator (%) does.
- Use % to find remainder after integer division.
- Understand % with different integer data types.
- Use % for even/odd checks and periodic logic.
- Avoid divide-by-zero and common mistakes with % usage.
Concept Explanation
What is the Remainder Operator (%)
The % operator gives the remainder left after integer division.
Example: 7 % 3 gives 1 because 7 divided by 3 leaves 1.
Remainder Operator Syntax
remainder = a % b;
if (value % 2 == 0) { /* even */ }How % Works
- Divide left operand by right operand using integer division.
- Take only the leftover part.
- Return that leftover as the result.
Think of this as "how much is left after equal grouping."
Finding Remainder with %
10 % 3 = 112 % 4 = 019 % 5 = 4
% with Different Data Types
int % intis valid.long % intis valid.float % floatis not supported with normal % operator in Arduino C++.
| Expression | Valid? | Result |
|---|---|---|
10 % 3 | Yes | 1 |
25L % 4 | Yes | 1 |
5.5 % 2.0 | No | Use integer logic or dedicated float math functions |
% vs Modulo Concept
In beginner Arduino usage, remainder and modulo are often treated similarly. Here, % returns leftover after integer division.
For positive values in Arduino lessons, this behavior is usually enough to build reliable cycle-based logic.
Common Use: Even or Odd Check
A number is even if number % 2 == 0. Otherwise, it is odd.
When to Use %
- Even/odd detection
- Run task every N cycles
- Circular index wrapping
- Pattern-based blinking logic
Division by Zero Safety
Always check divisor before using %. value % 0 is invalid.
if (divisor != 0) {
remainder = value % divisor;
} else {
remainder = 0;
}Example Code
This sketch uses % to check even/odd cycles and toggle LED state.
int ledPin = 2;
int cycle = 0;
int divisor = 2;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
}
void loop() {
int remainder = cycle % divisor;
if (remainder == 0) {
digitalWrite(ledPin, HIGH);
Serial.println("Even cycle -> LED ON");
} else {
digitalWrite(ledPin, LOW);
Serial.println("Odd cycle -> LED OFF");
}
Serial.print("cycle = ");
Serial.println(cycle);
Serial.print("remainder = ");
Serial.println(remainder);
cycle = cycle + 1;
delay(400);
}Example Code Explanation
remainder = cycle % divisorcomputes leftover after dividing cycle by 2.- When remainder is
0, cycle is even and LED turns ON. - When remainder is
1, cycle is odd and LED turns OFF. - Serial prints both cycle and remainder values so you can verify logic.
cycle = cycle + 1moves to next iteration and repeats the pattern.
Real-life analogy
Imagine arranging students in groups of 2. If one student is left without a pair, remainder is 1. If no one is left, remainder is 0.
What Happens Inside
- CPU performs integer division on operands.
- Quotient is computed but not returned to your variable.
- Leftover value (remainder) is extracted.
- Remainder is stored in destination variable.
- Program uses this remainder in
if/elseconditions.
Common Mistakes with %
- Trying to use
%with float values directly. - Using zero as divisor (
value % 0is invalid). - Assuming % returns quotient instead of remainder.
- Forgetting remainder logic may differ for negative values.
Best Practices for %
- Use integer types with
%. - Always ensure divisor is not zero.
- Use clear names like
remainderfor readability. - Print remainder in Serial while debugging loop logic.
- Prefer positive divisors in beginner sketches for predictable patterns.
Practice Task
- Change divisor from
2to3and observe new ON/OFF pattern. - Run LED ON only when
cycle % 3 == 0. - Add safe check for divisor before computing remainder.
- Print
cycle % 5and observe repeating sequence0..4.
Try it now
Open the simulator workspace and observe remainder-based LED behavior step by step.