Java Tutorial
🔍
What is OOP?Classes & ObjectsConstructorsAccess Modifiersthis Keywordstatic KeywordEncapsulationInheritancesuper KeywordMethod OverridingOverloading vs OverridingPolymorphismUpcasting & Downcastinginstanceof OperatorAbstractionAbstract ClassesInterfacesMarker InterfacesAbstract Class vs InterfaceObject ClasstoString() Methodequals() & hashCode()
Collections OverviewCollections HierarchyIterable InterfaceCollection InterfaceMap Interfaceequals() and hashCode()IteratorListIteratorFail-fast vs Fail-safe IteratorConcurrentModificationExceptionArrayListLinkedListHashSetLinkedHashSetTreeSetQueuePriorityQueueDequeArrayDequeHashMapLinkedHashMapTreeMapConcurrentHashMapCopyOnWriteArrayListList vs Set vs MapChoosing the Right CollectionComparableComparatorComparable vs ComparatorCollections Utility ClassArrays Utility ClassImmutable CollectionsCollection vs CollectionsVectorHashtableStackArrayList Internal WorkingLinkedList Internal WorkingHashMap Internal WorkingTreeMap Internal Working
Calculate Remainder in Java
beginner· Basics & I/O · Arithmetic
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.
Input
17, 5
Output
Remainder: 2
Java Program
Java
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
Remainder: 2
Core Logic
The % operator divides one number by another and returns just the leftover remainder, instead of the quotient.
How It Works
- 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.
With
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
% operatormoduloint
Approach 2: Manual Calculation Without %
Java
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
Remainder: 2
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.
How It Works
- 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.
For 17 and 5: quotient = 3, 3 * 5 = 15, and 17 - 15 = 2 — the same result
% 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
integer divisionarithmetic identity