Learn how to check whether a number is divisible by 10 in Java using the modulus operator . The modulus operator returns the remainder after division. A remainder of 0 indicates the number is divisible.
Code Explanation (Step-by-Step)
- Create a test() method that accepts an integer parameter.
- Use the modulus operator (%) to find the remainder when the number is divided by 10.
- Store the remainder in the dt variable.
- Use an if statement to check whether dt is equal to 0.
- If the remainder is 0, the number is divisible by 10.
- Otherwise, the number is not divisible by 10.
- Display the appropriate message based on the result.
public class DivisibleBy10 {
public static void test(int j) {
int dt = j % 10;
if (dt == 0) {
System.out.println("Number is divisible by 10");
} else {
System.out.println("Number is not divisible by 10");
}
}
public static void main(String[] args) {
int value = 143000;
test(value);
}
}
Output :-
Number is divisible by 10
Can negative numbers be divisible by 10?
public class NegativeDivisibleBy10 {
public static void test(int number) {
if (number % 10 == 0) {
System.out.println(number + " is divisible by 10");
} else {
System.out.println(number + " is not divisible by 10");
}
}
public static void main(String[] args) {
test(-50);
test(-37);
}
}
Output :-
-50 is divisible by 10
-37 is not divisible by 10
Here are some Java-related programs that will help you understand Java concepts better: