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 5 of 5

Thread: Need help with nested for loops to print the following number pattern

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with nested for loops to print the following number pattern

    1
    2, 1
    1, 2, 3
    4, 3, 2, 1
    ...............
    .................
    ....................
    ...................... (upto 10)

    notice the absence of comma at the end of the last integer
    Can someone please help me.
    Thanks in advance


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Need help with nested for loops to print the following number pattern

    Quote Originally Posted by xdx View Post
    ...notice the absence of comma at the end of the last integer...
    Well, here's the deal: Assuming this is the desired output of a program, every number will be printed because an instruction in the program prints it.

    Furthermore...

    Every comma will be printed because an instruction in the program prints it.

    So...

    If you don't want a commas at the end of the last integer on the lines, then don't put instructions in the program to print them.

    If we could see your code, maybe we could understand why it prints stuff that you don't want (assuming that's the kind of help you would like).



    Cheers!

    Z
    Last edited by Zaphod_b; September 4th, 2012 at 10:25 PM.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help with nested for loops to print the following number pattern

    In a loop, just create a check if it's 1 don't follow it with comma, else keep printing the number following comma.
    Remember, if it's 1, if it's other than 1, i hope you can make the logic.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Need help with nested for loops to print the following number pattern

    Quote Originally Posted by Mr.777 View Post
    In a loop, just create a check if it's 1 don't follow it with comma, else keep printing the number following comma.
    Remember, if it's 1, if it's other than 1, i hope you can make the logic.
    I disagree.
    Under that logic the lines that count up (ie line 3: 1,2,3) would have a comma at the end because they do not end with a 1

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Need help with nested for loops to print the following number pattern

    Ops yeah it's first ascending then descending. Yeah you are right jps.
    Yeah then that would become for getting the size if let's assume (i is dealing in the loop) i has the value which is almost gonna end up the loop after the iteration, don't put comma otherwise continue with the other statements.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young. The greatest thing in life is to keep your mind young.

    - Henry Ford

Similar Threads

  1. Nested for loops
    By Fordy252 in forum Loops & Control Statements
    Replies: 2
    Last Post: December 8th, 2012, 11:43 PM
  2. Nested for loops and array
    By bryanboy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 15th, 2011, 06:45 AM
  3. Using Nested Loops
    By m2msucks in forum Loops & Control Statements
    Replies: 7
    Last Post: November 5th, 2011, 07:05 PM
  4. Help with Nested Loops
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 03:31 PM
  5. to print pattern
    By Charanleen in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 3rd, 2010, 07:54 AM