Operators

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

  1. Divide left operand by right operand using integer division.
  2. Take only the leftover part.
  3. Return that leftover as the result.

Think of this as "how much is left after equal grouping."

Finding Remainder with %

  • 10 % 3 = 1
  • 12 % 4 = 0
  • 19 % 5 = 4

% with Different Data Types

  • int % int is valid.
  • long % int is valid.
  • float % float is not supported with normal % operator in Arduino C++.
ExpressionValid?Result
10 % 3Yes1
25L % 4Yes1
5.5 % 2.0NoUse 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

  1. remainder = cycle % divisor computes leftover after dividing cycle by 2.
  2. When remainder is 0, cycle is even and LED turns ON.
  3. When remainder is 1, cycle is odd and LED turns OFF.
  4. Serial prints both cycle and remainder values so you can verify logic.
  5. cycle = cycle + 1 moves 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

  1. CPU performs integer division on operands.
  2. Quotient is computed but not returned to your variable.
  3. Leftover value (remainder) is extracted.
  4. Remainder is stored in destination variable.
  5. Program uses this remainder in if/else conditions.

Common Mistakes with %

  • Trying to use % with float values directly.
  • Using zero as divisor (value % 0 is 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 remainder for readability.
  • Print remainder in Serial while debugging loop logic.
  • Prefer positive divisors in beginner sketches for predictable patterns.

Practice Task

  1. Change divisor from 2 to 3 and observe new ON/OFF pattern.
  2. Run LED ON only when cycle % 3 == 0.
  3. Add safe check for divisor before computing remainder.
  4. Print cycle % 5 and observe repeating sequence 0..4.

Try it now

Open the simulator workspace and observe remainder-based LED behavior step by step.

Run in Simulator