Results 1 to 10 of 10
  1. #1
    mp3_1 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default 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 08:05 PM. Reason: fixed a silly typo ...
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: Print Odd Multiples of 3

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

  4. #4
    mp3_1 is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Print Odd Multiples of 3

    Quote Originally Posted by joeyvitoro View Post
    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. #5
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default Re: Print Odd Multiples of 3

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

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Print Odd Multiples of 3

    Quote Originally Posted by joeyvitoro View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Print Odd Multiples of 3

    Quote Originally Posted by joeyvitoro View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    joeyvitoro is offline Member
    Join Date
    Oct 2011
    Posts
    90
    Rep Power
    0

    Default 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. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Print Odd Multiples of 3

    Quote Originally Posted by joeyvitoro View Post
    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
    If it's the thread about rendering cells in a JTable, I thought Fubarable answered in in his reply #5.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Why won't this print?
    By ozzyman in forum Java 2D
    Replies: 0
    Last Post: 05-03-2011, 01:20 AM
  2. Need help with print
    By lala in forum New To Java
    Replies: 0
    Last Post: 11-25-2010, 05:24 PM
  3. Multiples Integers from a String
    By AndrewM16921 in forum New To Java
    Replies: 3
    Last Post: 04-01-2009, 12:00 AM
  4. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM

Posting Permissions

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