In this tutorial, you will learn how to write Java programs to check divisibility. We will explore how to determine whether a number is divisible by another—such as checking if a number is divisible by 5, or by both 5 and 11 simultaneously.
To achieve this, we will use the modulus operator (%) combined with if-else statements to check whether a division leaves a remainder. We will also look at how to use the Scanner class to accept dynamic user input from the console. Each approach includes a complete Java program, expected output, and a detailed explanation.
What Does Divisible Mean?
A number is said to be divisible by another number if it can be divided completely without leaving a remainder.
For example:
- 20 is divisible by 5 because 20 / 5 = 4 with a remainder of 0.
- 15 is divisible by 3 because 15 / 3 = 5 with a remainder of 0.
- 17 is not divisible by 5 because 17 / 5 leaves a remainder of 2.
- 24 is not divisible by 7 because 24 / 7 leaves a remainder of 3.
In Java, we can test whether one integer is divisible by another by checking if the remainder is zero using the modulus operator (%).
How to Check Whether a Number Is Divisible by Another in Java
Java provides the modulus operator (%), which returns the remainder of a division operation. We can use it to determine whether a number is exactly divisible by another.
The basic condition is:
number % divisor == 0
Here:
numberrepresents the number to be checked.divisorrepresents the number by which we want to divide it.%calculates the remainder after division.== 0checks whether the remainder is zero.
If the remainder is zero, the number is divisible by the divisor. Otherwise, it is not divisible. For example:
System.out.println(20 % 5); // Output: 0 System.out.println(17 % 5); // Output: 2
In the first statement, 20 % 5 returns 0, meaning 20 is divisible by 5. In the second, 17 % 5 returns 2, meaning 17 is not divisible by 5.
Note: The divisor must never be zero. For integer operands, evaluating an expression like 20 % 0 will throw a java.lang.ArithmeticException.
Java Program to Check Whether a Number Is Divisible by Another
Let us first write a simple Java program to check whether the number 20 is divisible by 5. In this approach, we assign the values to variables directly, then use an if-else statement and the modulus operator to verify divisibility.
Example 1: Number 20 is Divisible by 5
// Java program to check whether a number is divisible by another number
public class DivisibilityCheck {
public static void main(String[] args) {
// Declare and initialize two integer numbers
int number = 20;
int divisor = 5;
// Check whether the divisor is zero to prevent ArithmeticException
if (divisor == 0) {
System.out.println("Division by zero is not allowed.");
}
// Check whether the number is divisible by the divisor
else if (number % divisor == 0) {
System.out.println(number + " is divisible by " + divisor + ".");
}
// Execute when the remainder is not zero
else {
System.out.println(number + " is not divisible by " + divisor + ".");
}
}
}Output:
20 is divisible by 5.
In this example:
- We declare two integer variables, number and divisor, and initialize them with the values 20 and 5, respectively.
- The first if statement checks whether the divisor is zero. This prevents an invalid remainder operation.
- The else if condition, number % divisor == 0, calculates the remainder and checks whether it equals zero.
- If the condition is true, the program displays that the number is divisible by the divisor.
- If the condition is false, the else block displays that the number is not divisible by the divisor.
In this example, 20 % 5 returns 0. Therefore, the program displays that 20 is divisible by 5. You can change the values of number and divisor to test different cases.
Java Program to Check Whether a Number Is Divisible by Both 5 and 11
In this example, we will learn how to write a Java program to check whether a number is divisible by both 5 and 11. A number is divisible by both numbers if the remainder is zero when divided by 5 and when divided by 11.
We will use the modulus operator (%) and the if-else statement to check both conditions. If both conditions are true, the program will display that the number is divisible by both 5 and 11. Otherwise, it will display that the number is not divisible by both numbers.
Example 2: Number is Divisible by Both 5 and 11
// Java program to check whether a number is divisible by both 5 and 11
public class DivisibleByFiveAndEleven {
public static void main(String[] args) {
// Declare and initialize the number to be checked
int number = 55;
// Check whether the number is divisible by both 5 and 11 using the logical AND (&&) operator
if (number % 5 == 0 && number % 11 == 0) {
System.out.println(number + " is divisible by both 5 and 11.");
} else {
System.out.println(number + " is not divisible by both 5 and 11.");
}
}
}Output:
55 is divisible by both 5 and 11.
In this example:
- We declare an integer variable number and initialize it with 55.
- The expression number % 5 == 0 checks whether the number is divisible by 5.
- The expression number % 11 == 0 checks whether the number is divisible by 11.
- The logical AND operator (&&) ensures that both conditions must be true.
- If both conditions are true, the if block displays that the number is divisible by both 5 and 11. Otherwise, the else block displays the alternative message.
For example, 55 is divisible by both 5 and 11 because 55 % 5 and 55 % 11 both return 0.
Java Program to Check Divisibility Using Scanner
In the previous example, we assigned values directly to variables. However, we often need to accept numbers from the user at runtime in the applications or project.
Java provides the Scanner class in the java.util package to read input from the user’s keyboard. We can use its nextInt() method to read two integer values and check whether the first is divisible by the second.
Example 3: Check Divisibility Using Scanner
// Java program to check divisibility using the Scanner class for user input
import java.util.Scanner;
public class DivisibilityUsingScanner {
public static void main(String[] args) {
// Create a Scanner object to read input from the console
Scanner sc = new Scanner(System.in);
// Prompt and read the number to be checked
System.out.print("Enter the number: ");
int number = sc.nextInt();
// Prompt and read the divisor
System.out.print("Enter the divisor: ");
int divisor = sc.nextInt();
// Check whether the divisor is zero to prevent ArithmeticException
if (divisor == 0) {
System.out.println("Division by zero is not allowed.");
}
// Check whether the number is divisible by the divisor
else if (number % divisor == 0) {
System.out.println(number + " is divisible by " + divisor + ".");
}
// Execute when the remainder is not zero
else {
System.out.println(number + " is not divisible by " + divisor + ".");
}
// Close the Scanner object to free system resources
sc.close();
}
}Output 1:
Enter the number: 36 Enter the divisor: 6 36 is divisible by 6.
Output 2:
Enter the number: 25 Enter the divisor: 4 25 is not divisible by 4.
In this example:
import java.util.Scanner;imports the Scanner class required to read input from the user’s keyboard.- The statement
Scanner sc = new Scanner(System.in);creates a Scanner object connected to the keyboard input. - The
sc.nextInt()method reads an integer value entered by the user. - The program checks whether
divisor == 0before performing the remainder operation. - The expression
number % divisor == 0determines whether the first number is divisible by the second. - The if-else statement displays the appropriate result.
- Finally,
sc.close()closes the Scanner object.
Common Mistakes to Avoid
When writing a Java program to check divisibility, keep the following points in mind.
1. Using the division operator (/) instead of the modulus operator (%)
The division operator calculates the quotient, whereas the modulus operator calculates the remainder. To check exact divisibility, use %.
2. Using = instead of ==
The expression number % divisor == 0 compares the remainder with zero. A single equals sign (=) is used for assignment, not equality comparison.
3. Forgetting to check whether the divisor is zero
An expression such as number % 0 throws an ArithmeticException for integer operands. Always validate the divisor before calculating the remainder.
4. Assuming every nonzero remainder is positive
In Java, the remainder can be negative when the dividend is negative. For example, -7 % 3 returns -1. Therefore, checking number % divisor != 0 is a better way to determine that a number is not exactly divisible than checking whether the remainder is positive.







