I need to create a boolean variable called hasOddFactor that is true if num is divisible by any odd number greater than 1 (including possibly itself), and false otherwise. The current code that I can only tell if a number is even or odd and I'm confused on how to get it to come up as true and false if the number is divisible by an odd number or not.
Current code:
boolean hasOddFactor = false;
for(int i = 0; i < num; i++){
if(num % 2 == 0 || num % 13 == 0){
hasOddFactor = true;
}
else{
hasOddFactor = false;
}
}
I just need it to come up as true for the numbers 13, 26, and 27, but false for 16.
try this code
boolean hasOddFactor = false;
for (int i = 3; i <= num; i += 2) {
if (num % i == 0) {
hasOddFactor = true;
break;
} }