# Thread: Print Odd Multiples of 3

1. Member
Join Date
Nov 2011
Posts
10
Rep Power
0

## 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.

Java Code:
```       int multOfThree = 0;
for (int i = 1; i < 67; i++)
{
multOfThree += 3;

System.out.println(multOfThree);
i++;

}```

2. ## 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
Last edited by JosAH; 11-22-2011 at 09:05 PM. Reason: fixed a silly typo ...

3. Member
Join Date
Oct 2011
Posts
90
Rep Power
0

## Re: Print Odd Multiples of 3

You could also use the modulus operator.
Java Code:
```if (x % 2 != 0)
{
// odd
}```

4. Member
Join Date
Nov 2011
Posts
10
Rep Power
0

## Re: Print Odd Multiples of 3

Originally Posted by joeyvitoro
You could also use the modulus operator.
Java 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.

5. Member
Join Date
Oct 2011
Posts
90
Rep Power
0

## Re: Print Odd Multiples of 3

That might have been a typo, but (true != true) will always evaluate to be false.

6. ## Re: Print Odd Multiples of 3

Originally Posted by joeyvitoro
You could also use the modulus operator.
Java Code:
```if (x % 2 != 0)
{
// odd
}```
Sure, why implement an efficient solution while you can hack something together?

kind regards,

Jos

7. Member
Join Date
Oct 2011
Posts
90
Rep Power
0

## 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.

8. ## Re: Print Odd Multiples of 3

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

9. Member
Join Date
Oct 2011
Posts
90
Rep Power
0

## 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

10. ## Re: Print Odd Multiples of 3

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

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•