0

I'm trying to print out all odd numbers that aren't multiples of 7 or 9. It works by seeing if the remainder is first not 0 when divided by two, giving the odd numbers.

But when I've put it to show the numbers if they are not multiples of 7 it just displays ALL the odd numbers, have I made a mistake?

public class NoMultiples7and9 {

    public static void main(String[] args) {

        for (int i = 1; i <= 30; i++) {

            if (i % 2 != 0) {

                if (i % 7 != 0 || i % 9 != 0) {

                    System.out.println(i);
                }
            }
        }
    }
}
2
  • This looks like homework, so I'll just give a hint. What does i % 7 !=0 || i % 9 != 0 evaluate if i is a multiple of only 7 (e.g. 21)? What should it evaluate to? Get that working correctly and the rest should fall out. Commented Nov 20, 2011 at 15:33
  • It would print it because although 21 is divisible by 7, it's not divisible by 9 so I'd need to set it to when it's not divisible by 7 and not divisible by 9. Commented Nov 20, 2011 at 19:32

4 Answers 4

3

Change your code with:

for (int i = 1; i <= 30; i = i + 2) {
   if (i % 7 != 0 && i % 9 != 0) {
      System.out.println(i);
   }
}

Please note the use of the && (AND) instead of the || (OR) and the useless of the i % 2 because you can loop only on the odd numbers by changing a little bit the for loop.

Sign up to request clarification or add additional context in comments.

Comments

2

You need to use AND instead of OR in your comparison. In the comparison i % 7 != 0 || i % 9 != 0, even if i mod 7 is 0, i mod 9 may not be and vice versa.

1 Comment

on homeworkquestions prefer learn-how-to-fish type answers rather than giving away free fish.
1

your inner if statement is wrong, it will cause all odd numbers that aren't divisible by by both 7 and 9 to be printed. I bet if you change your loop to go to 63 it won't print 63. The initial % 2 check is also not needed.

public class NoMultiples7and9 {

    public static void main(String[] args) {

        for (int i = 1; i <= 30; i++) {

            if (i % 7 != 0 && i % 9 != 0) {

                System.out.println(i);

            }
        }
    }
}

1 Comment

on homeworkquestions prefer learn-how-to-fish type answers rather than giving away free fish.
0
for (i = 1; i <= 30; i++) {  
        if (i % 2 != 0) {
            if(i % 7 != 0) {
                if(i % 9 != 0)
                    System.out.println(i);
            }

        }
 }

3 Comments

Isn't this solution a little bit ugly (I mean too unnecessary nested if)?
True but you're teaching very ugly pratice. To do 5 + 3 you can also use a = 5 for(i = 1; i <= 3;i++) a++; It works but you know you should not do this kind of stuff.
this is a homework question and this is pedagogically troublesome.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.