Modulo Operator

The modulo operator (%) in Java returns the remainder after division, so 17 % 5 evaluates to 2. On AP Computer Science A, it shows up constantly in arithmetic expressions for checking divisibility (n % 2 == 0 means even), extracting digits, and wrapping values around a fixed range.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is the Modulo Operator?

The modulo operator, written as %, is one of Java's five arithmetic operators (along with +, -, *, and /). It gives you the remainder of a division. So 17 % 5 is 2, because 5 goes into 17 three times with 2 left over. Think of it as the leftover after integer division does its thing. In fact, modulo and integer division are partners. 17 / 5 tells you how many whole times 5 fits (3), and 17 % 5 tells you what's left (2).

The two patterns you'll use over and over are divisibility checks and digit extraction. If n % d == 0, then n is evenly divisible by d, which is how you test for even numbers (n % 2 == 0). Meanwhile, n % 10 peels off the last digit of a number, and pairing it with n / 10 (which chops that digit off) lets you walk through a number digit by digit. Modulo also "wraps" values around a range, which is why it powers things like circular array indexing and clock arithmetic. One Java-specific detail worth knowing is that % works on doubles too, but on the AP exam you'll almost always see it with ints.

Why the Modulo Operator matters in AP Computer Science A

Modulo lives in Unit 1 (Primitive Types) as part of arithmetic expressions, but it refuses to stay there. It reappears in Unit 3 when boolean expressions like n % 2 == 0 drive if-statements, and it's everywhere in Unit 4 loops, where digit-extraction loops (n % 10 then n /= 10) are a classic algorithm pattern the CED explicitly calls out. If you can't trace % quickly and correctly, you'll lose easy points on code-tracing multiple choice questions and stumble on FRQ algorithms that hinge on remainders. The good news is that it's mechanical. Once you internalize "remainder after division," the operator never surprises you again.

How the Modulo Operator connects across the course

Integer Division (Unit 1)

These two operators are a matched set. / with two ints gives the whole-number quotient and throws away the remainder, while % gives you exactly the part that / threw away. So 17 / 5 is 3 and 17 % 5 is 2, and together they fully describe the division.

Boolean expressions and conditionals (Unit 3)

Modulo's most common job is feeding a comparison. n % 2 == 0 is the standard even-number test, and year % 4 == 0 style checks show up in divisibility logic. The pattern is always the same. A remainder of zero means it divides evenly.

Loop algorithms and digit manipulation (Unit 4)

The CED's standard algorithms include processing the digits of a number, and that whole pattern runs on modulo. Inside a while loop, n % 10 grabs the last digit and n / 10 removes it, repeating until n hits zero. This combo shows up in sum-of-digits, reverse-a-number, and palindrome problems.

Remainder (Unit 1)

"Remainder" is the math concept and % is the Java operator that computes it. When a question asks what's left over after division, modulo is the tool you reach for.

Is the Modulo Operator on the AP Computer Science A exam?

On the multiple-choice section, modulo appears in expression-evaluation questions ("What does 23 % 7 evaluate to?") and inside code-tracing problems where a loop uses % to filter values or extract digits. A wrong answer choice will almost always be the quotient instead of the remainder, so know which operator gives which. On the free-response section, no prompt will say "use modulo," but problems often need it. If an FRQ asks you to count even numbers, check divisibility, process digits of an int, or cycle through positions in a fixed range, % is the cleanest solution. Practice tracing expressions like x % 10 and x % 2 by hand until they're automatic, because exam timing doesn't leave room to second-guess arithmetic.

The Modulo Operator vs Integer Division

With two int operands, / gives the quotient (how many whole times the divisor fits) and % gives the remainder (what's left over). So 7 / 2 is 3, while 7 % 2 is 1. Mixing these up is the single most common modulo error on the exam. A quick sanity check helps. The result of a % b (for positive values) is always between 0 and b - 1, so if your "remainder" is bigger than the divisor, you computed the quotient by mistake.

Key things to remember about the Modulo Operator

  • The modulo operator % returns the remainder of a division, so 17 % 5 equals 2.

  • If n % d equals 0, then n is evenly divisible by d, which is how you check for even numbers with n % 2 == 0.

  • For positive operands, a % b always produces a result from 0 up to b - 1, never the divisor itself or larger.

  • n % 10 gives you the last digit of an integer, and pairing it with n / 10 in a loop lets you process a number digit by digit.

  • Don't confuse % with integer division: 7 / 2 is 3 (the quotient) while 7 % 2 is 1 (the remainder).

  • Modulo has the same precedence as multiplication and division, so expressions evaluate left to right among *, /, and %.

Frequently asked questions about the Modulo Operator

What is the modulo operator in AP Computer Science A?

The modulo operator (%) is a Java arithmetic operator that returns the remainder of a division. For example, 17 % 5 evaluates to 2 because 5 fits into 17 three times with 2 left over.

Is modulo the same as division in Java?

No. Integer division (/) gives you the quotient, while modulo (%) gives you the remainder. 7 / 2 is 3, but 7 % 2 is 1, and exam questions love putting both as answer choices to catch students who mix them up.

How do you check if a number is even or odd in Java?

Use modulo with 2. If n % 2 == 0, the number is even; if n % 2 == 1 (for positive n), it's odd. This is one of the most common modulo patterns on AP CSA multiple choice and FRQs.

Can the result of % be bigger than the divisor?

No. For positive operands, a % b always lands between 0 and b - 1. If you trace an expression and get a remainder larger than the divisor, you accidentally computed the quotient instead.

Why is n % 10 used so often in AP CSA code?

Because n % 10 returns the last digit of an integer. Combined with n / 10, which drops that digit, you can loop through every digit of a number, a pattern used in sum-of-digits and reverse-a-number algorithms in Unit 4.