Combining modulo with range checks.
Test divisibility and bounds in one compound condition — the building block of "every nth in range" problems.
Modulo (%) tests divisibility: n % k == 0 is true when k divides n exactly. Range conditions bound a value between a lower and upper limit.
Together they answer questions like "count multiples of 3 between 10 and 50" or "list even numbers from 1 to 100". The combined boolean uses && to chain both tests.
On the AP exam, this combination appears in tracing MCQs and in FRQs that filter by divisibility within a range.
One compound condition handles both tests.
&& chains divisibility with bounds.
The template
if (n % k == 0 && n >= lo && n <= hi) {
// n is a multiple of k in [lo, hi]
}
Concrete example: count multiples of 3 from 1 to 100
int count = 0;
for (int n = 1; n <= 100; n++) {
if (n % 3 == 0) count++;
}
// count = 33
Counting only when both pass
public int countMultiplesInRange(int[] arr, int k, int lo, int hi) {
int count = 0;
for (int n : arr) {
if (n % k == 0 && n >= lo && n <= hi) {
count++;
}
}
return count;
}
Inclusive vs exclusive bounds
n >= loincludes the lower endpoint.n <= hiincludes the upper endpoint.- Use
>or<for exclusive endpoints.
Modulo + range vocabulary.
Used in divisibility-filter problems.
- Modulo (
%) — operator returning the remainder. - Divisibility —
n % k == 0meanskdividesn. - Range condition — bounded by lo and hi.
- Inclusive — endpoint included via
>=/<=. - Exclusive — endpoint excluded via
>/<. - Compound condition — multiple tests joined by
&&or||.
How modulo + range is tested.
Endpoint inclusion and modulo on negatives dominate.
- Inclusive vs exclusive boundary. "Between 10 and 20" usually includes both.
- Modulo on negatives.
-7 % 3is-1in Java, not2. - Order of operands.
0 == n % kworks too, butn % k == 0reads naturally. - && short-circuits. If the modulo check fails, range check is skipped.
Count multiples of 5 in [20, 40].
Compound condition in action.
Input: [5, 25, 30, 38, 40, 45]; k=5; lo=20; hi=40.
Trace.
- 5: % 5 == 0 yes; 5 ≥ 20 no → skip.
- 25: % 5 == 0 yes; 25 in [20, 40] yes → count.
- 30: yes and yes → count.
- 38: % 5 == 0 no → skip.
- 40: yes and yes → count.
- 45: yes; 45 ≤ 40 no → skip.
Answer: 3.
Modulo and range pitfalls.
Most are endpoint or operator errors.
- Wrong endpoint inclusion. "Less than 50" excludes 50; "at most 50" includes it.
- Negative modulo confusion. Java keeps the sign of the dividend.
- Using
||instead of&&. Wrong logic — must satisfy both. - Comparing
n % k = 0. Assignment, not comparison; use==.
Modulo + range at a glance.
Boundary inclusion summary.
Modulo & Range Reference
AP Quick Reference| Test | Code | Meaning |
|---|---|---|
| Divisible | n % k == 0 | k divides n. |
| Inclusive range | n >= lo && n <= hi | Both ends included. |
| Half-open | n >= lo && n < hi | Hi excluded. |
| Combined | n % k == 0 && range | Both must hold. |
How to master compound conditions.
Underline the prompt's boundary words.
- MCQ Practice — check both sides of the AND; check endpoint inclusion.
- Java Lab — count multiples in ranges; test with negative numbers.
- FRQ Practice — write the boundary words into a comment before coding the condition.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 09 Topic 02 Modulo and Range Conditions FRQ Practice
AP-style topic practice assessment