Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: pattern by for loop

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default pattern by for loop

    Iam getting output

    222

    44444

    6666666

    888888888

    1010101010101010101010
    I want

    0
    2 4
    6 8 10
    12 14 16 18


    my code is

    class Try7{


    public static void main( String arg[]){
    for(int i=0;i<=10;i++){


    for(int j=0;j<=i;j++){
    if(i%2==0){
    System.out.print(i);
    }

    }
    System.out.println("");
    }

    }
    }
    Last edited by ssonia; August 10th, 2014 at 01:24 AM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: pattern by for loop

    In that case change your code!
    Improving the world one idiot at a time!

  3. #3
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: pattern by for loop

    As a starting point, your expected output highest value is 18.
    Your counting only goes as high as 10. If you want the second value
    of every even integer found - you need to change your increment operator
    within your for loop so it counts in 2's.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: pattern by for loop

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

Similar Threads

  1. pattern
    By AJAY KAMBLE in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 16th, 2014, 04:21 AM
  2. diamond pattern and loop
    By Naif in forum Loops & Control Statements
    Replies: 4
    Last Post: October 31st, 2011, 10:51 PM
  3. Regular Expression pattern - complex pattern syntax
    By SmartAndy in forum Algorithms & Recursion
    Replies: 3
    Last Post: June 7th, 2011, 04:40 AM
  4. to print pattern
    By Charanleen in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 3rd, 2010, 07:54 AM
  5. Stuck -- Loop Pattern
    By CodeNewb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 11th, 2010, 03:02 AM