LESSON 18
3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Define the modulus operator in Java.
- Use the modulus operator in calculations.
- Determine remainders using modulus operations.
- Apply modulus operations in programming logic.
- Use the modulus operator in Java applications.
3.2 Overview
Java supports several arithmetic operators used for mathematical calculations and logical processing. One of these operators is the modulus operator, which calculates the remainder after division.
This lesson introduces learners to the modulus operator and explains how modulus operations are used in calculations, validations, conditions, and algorithms. Learners will also explore how the modulus operator supports problem-solving in Java programming.
The modulus operator is commonly used in:
- banking systems,
- scheduling systems,
- gaming applications,
- inventory systems,
- and enterprise software.
Understanding the modulus operator is important because it supports mathematical processing and logical decision making in Java applications.
KT1801 — Introduction to the Modulus Operator
The modulus operator is represented using the % symbol.
The modulus operator returns the remainder after division.
Syntax
number1 % number2
Example
public class Main {
public static void main(String[] args) {
int result = 10 % 3;
System.out.println(result);
}
}
Output
1
Explanation
10 divided by 3 gives:
- quotient = 3
- remainder = 1
The modulus operator returns the remainder.
Importance of the Modulus Operator
The modulus operator helps developers:
- calculate remainders,
- create conditions,
- validate data,
- and solve programming problems.
KT1802 — Using Modulus with Even and Odd Numbers
The modulus operator is commonly used to determine whether numbers are even or odd.
Even Number Check
number % 2 == 0
If the remainder is 0:
- the number is even.
Odd Number Check
number % 2 != 0
If the remainder is not 0:
- the number is odd.
Example
public class Main {
public static void main(String[] args) {
int number = 7;
if(number % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
}
}
Output
Odd
Importance of Even and Odd Validation
Even and odd checks are used in:
- mathematical systems,
- games,
- and algorithm processing.
KT1803 — Using Modulus for Divisibility
The modulus operator determines whether one number is divisible by another.
Divisibility Rule
number % divisor == 0
If the remainder is 0:
- the number is divisible.
Example
public class Main {
public static void main(String[] args) {
int number = 15;
if(number % 5 == 0) {
System.out.println("Divisible");
}
}
}
Output
Divisible
Importance of Divisibility Checks
Divisibility checks are used in:
- calculations,
- validations,
- scheduling,
- and algorithms.
KT1804 — Using Modulus in Loops
The modulus operator can filter values during loop processing.
Example
public class Main {
public static void main(String[] args) {
for(int i = 1; i <= 10; i++) {
if(i % 2 == 0) {
System.out.println(i);
}
}
}
}
Output
2
4
6
8
10
Importance of Modulus in Loops
The modulus operator helps:
- process conditions,
- filter numbers,
- and automate calculations.
KT1805 — Real-World Use of the Modulus Operator
The modulus operator is used in:
- ATM systems,
- gaming systems,
- scheduling applications,
- inventory systems,
- and enterprise software.
Examples:
- determining payment cycles,
- validating IDs,
- checking numeric patterns,
- and alternating records.
The modulus operator supports mathematical and logical processing in software applications.
3.5 Key Notes / Summary
- The modulus operator uses the
%symbol. - Modulus returns the remainder after division.
- Modulus is used for even and odd number checks.
- Modulus supports divisibility testing.
- Modulus can be used inside loops and algorithms.
- The modulus operator supports logical and mathematical operations.