# Print Odd Multiples of 3

• 11-22-2011, 06:37 PM
mp3_1
Print Odd Multiples of 3
I am having trouble on an assignment where I need to print the multiples of 3 between 1 and 100 and are odd, so the outcome would be...

3
9
15
21
...
99

I have coded the following which prints all multiples of 3 from 1 - 99, but I only want the odd multiples.

Code:

```      int multOfThree = 0;       for (int i = 1; i < 67; i++)       {         multOfThree += 3;               System.out.println(multOfThree);         i++;             }```
http://i44.tinypic.com/donzvq.png

• 11-22-2011, 06:40 PM
JosAH
Re: Print Odd Multiples of 3
So you want the numbers 3, 3+6, 3+6+6 ... 3+n*6 for n >= 0 and 3+n*6 < 100? hint: a for-loop can do it.

kind regards,

Jos
• 11-22-2011, 07:06 PM
joeyvitoro
Re: Print Odd Multiples of 3
You could also use the modulus operator.
Code:

```if (x % 2 != 0) {   // odd }```
• 11-22-2011, 07:16 PM
mp3_1
Re: Print Odd Multiples of 3
Quote:

Originally Posted by joeyvitoro
You could also use the modulus operator.
Code:

```if (x % 2 != 0) {   // odd }```

AHh I see now. multOfThree%2 produces the pattern I want for printing every other line (0,1,0,1,0), so, by making an if statement (true != true), it will print in that order.
• 11-22-2011, 07:48 PM
joeyvitoro
Re: Print Odd Multiples of 3
That might have been a typo, but (true != true) will always evaluate to be false.
• 11-22-2011, 09:07 PM
JosAH
Re: Print Odd Multiples of 3
Quote:

Originally Posted by joeyvitoro
You could also use the modulus operator.
Code:

```if (x % 2 != 0) {   // odd }```

Sure, why implement an efficient solution while you can hack something together?

kind regards,

Jos
• 11-22-2011, 09:38 PM
joeyvitoro
Re: Print Odd Multiples of 3
Oh Jos, you always seem so mean spirited. Your method was certainly easier, and would have worked for his needs, but now he knows how to find out if any number is odd or even.
• 11-22-2011, 09:47 PM
JosAH
Re: Print Odd Multiples of 3
Quote:

Originally Posted by joeyvitoro
Oh Jos, you always seem so mean spirited. Your method was certainly easier, and would have worked for his needs, but now he knows how to find out if any number is odd or even.

What is "mean spirited" about efficiency?

kind regards,

Jos
• 11-22-2011, 10:01 PM
joeyvitoro
Re: Print Odd Multiples of 3
I think you're smart enough to know the difference between style and substance ;)

However, I have a thread in advanced java that I need an answer too. If you can help me out, I'll gladly accept the sarcasm that comes with it! :D
• 11-22-2011, 10:09 PM
JosAH
Re: Print Odd Multiples of 3
Quote:

Originally Posted by joeyvitoro
I think you're smart enough to know the difference between style and substance ;)

However, I have a thread in advanced java that I need an answer too. If you can help me out, I'll gladly accept the sarcasm that comes with it! :D