Calculate Remainder in Java
Problem
The modulo operator (%) returns the remainder left over after dividing one number by another.
Given two integers, find the remainder when the first is divided by the second.
Java Program
public class CalculateRemainder {
public static void main(String[] args) {
int a = 17;
int b = 5;
int remainder = a % b; // leftover after integer division
System.out.println("Remainder: " + remainder);
}
}Output
Core Logic
The % operator divides one number by another and returns just the leftover remainder, instead of the quotient.
- 1
aholds17andbholds5. - 2
a % bevaluates to2— 5 goes into 17 three times (15), leaving 2 left over. - 3The result is stored in
remainderand printed with a label.
a = 17 and b = 5, a % b evaluates to 2, so the program prints "Remainder: 2".Key Point: % is also the standard way to check divisibility or evenness in Java — n % 2 == 0 tests whether n is even.
Key Concepts
Approach 2: Manual Calculation Without %
public class CalculateRemainderManual {
public static void main(String[] args) {
int a = 17;
int b = 5;
int quotient = a / b;
int remainder = a - (quotient * b); // same result as a % b
System.out.println("Remainder: " + remainder);
}
}
Output
Core Logic
The remainder can also be derived manually from division and multiplication, without the % operator at all — useful for understanding what % actually computes under the hood.
- 1
a / bperforms integer division first, giving the whole-number quotient —17 / 5is3. - 2Multiplying that quotient back by
b(3 * 5 = 15) gives the largest multiple ofbthat fits insidea. - 3Subtracting that from the original
a(17 - 15) leaves exactly the remainder,2.
% would give directly.Key Point: This is exactly the identity the % operator implements internally: a % b always equals a - (a / b) * b for positive integers.
Key Concepts
Approach 3: Java 8
import java.util.function.IntBinaryOperator;
public class CalculateRemainderLambda {
public static void main(String[] args) {
int a = 17;
int b = 5;
// The modulo logic is stored as a lambda, named for what it computes
IntBinaryOperator remainderOf = (x, y) -> x % y;
int remainder = remainderOf.applyAsInt(a, b);
System.out.println("Remainder: " + remainder);
}
}
Output
Core Logic
The % operator can be wrapped in a lambda the same way + and - were — a small step that turns 'compute a remainder' into a reusable value.
- 1
IntBinaryOperator remainderOf = (x, y) -> x % y;stores the modulo logic as a lambda. - 2
remainderOf.applyAsInt(a, b)calls it withaandb, returning the same resulta % bwould inline. - 3The result is printed exactly as before.
a = 17 and b = 5, remainderOf.applyAsInt(17, 5) returns 2.Key Point: Naming the lambda remainderOf instead of a generic op makes the intent clear at the call site — worth doing whenever a lambda is stored in a variable rather than passed inline.
Key Concepts
Interview Questions
What is the remainder of a negative number in Java?
It takes the sign of the dividend, so -7 % 3 is -1. Use Math.floorMod(-7, 3) to get the always-positive 2.
Does % work with decimals?
Yes. 7.5 % 2 is 1.5 — the modulo operator works on double values too.