Results 1 to 10 of 10
Thread: Print Odd Multiples of 3
 11222011, 06:37 PM #1Member
 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++; }
Thank you for your help.
 11222011, 06:40 PM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,421
 Blog Entries
 7
 Rep Power
 26
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 forloop can do it.
kind regards,
JosLast edited by JosAH; 11222011 at 09:05 PM. Reason: fixed a silly typo ...
Build a wall around Donald Trump; I'll pay for it.
 11222011, 07:06 PM #3Member
 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 }
 11222011, 07:16 PM #4Member
 Join Date
 Nov 2011
 Posts
 10
 Rep Power
 0
 11222011, 07:48 PM #5Member
 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.
 11222011, 09:07 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,421
 Blog Entries
 7
 Rep Power
 26
 11222011, 09:38 PM #7Member
 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.
 11222011, 09:47 PM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,421
 Blog Entries
 7
 Rep Power
 26
 11222011, 10:01 PM #9Member
 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
 11222011, 10:09 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,421
 Blog Entries
 7
 Rep Power
 26
Similar Threads

Why won't this print?
By ozzyman in forum Java 2DReplies: 0Last Post: 05032011, 02:20 AM 
Need help with print
By lala in forum New To JavaReplies: 0Last Post: 11252010, 06:24 PM 
Multiples Integers from a String
By AndrewM16921 in forum New To JavaReplies: 3Last Post: 04012009, 01:00 AM 
Print the text file and print preview them
By Java Tip in forum java.awtReplies: 0Last Post: 06232008, 12:04 AM
Bookmarks