I have an array with integers inside it. I want to iterate through them to check if its divisible by 2, 3, 5. At the moment my code only run once.
So say if I have 6 in the list. It will only return "6 is divisible by 2" where it should be "6 is divisible by 2 and 3"
So how do I make the code more elegant. Is there a way to write the code without having to define like if (number % 2 == 0) && (number % 3 == 0)... or it has to be done that way? defining each of the condition each time.
this is my code
public class checkingDivisibility {
public static void main(String[] args) {
int list[] = {1, 2, 3, 6, 8, 10, 12, 14, 15, 17, 19, 21};
for (int x : list) {
if (x % 2 == 0) {
System.out.println(x + "div by 2 possible");
} else if (x % 3 == 0) {
System.out.println(x + "div by 3 possible");
} else if (x % 5 == 0) {
System.out.println(x + "div by 5 possible");
}
}
}
}
elseclauses so theifs evaluate independently.