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

Thread: Nested for loops

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Location
    Ireland
    Posts
    15
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Nested for loops

    hello, i am new to java and i am having serious problems with nested loops. I just can't seem to get my head around them. The following pyramids are problems for homework and i am already stuck on the first pattern.. My attempted code is below and i can only seem to get the pyramid to print out the next number repeating on the next line.. I would really apreciate if someone could help me please and any tips for the best ways to go about solving these problems.. thank you..

    (Printing four patterns using loops) Use nested loops that print the following patterns in four separate programs:
    Pattern I       Pattern II     Pattern III       Pattern IV
    1               1 2 3 4 5 6              1              1 2 3 4 5 6
    1 2             1 2 3 4 5              2 1                1 2 3 4 5
    1 2 3           1 2 3 4              3 2 1                  1 2 3 4
    1 2 3 4         1 2 3              4 3 2 1                     1 2 3
    1 2 3 4 5       1 2              5 4 3 2 1                       1 2
    1 2 3 4 5 6     1              6 5 4 3 2 1                         1

    public class Numbers {
     
    	public static void main(String[] args) {
     
    		int i;
    		int j;
     
    		for ( i = 1; i < 7; i++) {
    			for ( j = 0; j < i; j++) {
    				System.out.print(i + " ");
    			}
    			System.out.println();
     
     
    		}
     
    	}
    }
    Last edited by Fordy252; November 1st, 2010 at 08:09 PM. Reason: formatting is out


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Nested for loops

    You are close.

    		for (int i = 1; i < 8; i++) {
    			for (int j = 1; j < i; j++) {
    				System.out.print(j + " ");
    			}
    			System.out.println("");
    		}

     

    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5
    1 2 3 4 5 6


    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Nested for loops

    [QUOTE=Fordy252;20042]hello, i am new to java and i am having serious problems with nested loops. I just can't seem to get my head around them. The following pyramids are problems for homework and i am already stuck on the first pattern.. My attempted code is below and i can only seem to get the pyramid to print out the next number repeating on the next line.. I would really apreciate if someone could help me please and any tips for the best ways to go about solving these problems.. thank you..

    (Printing four patterns using loops) Use nested loops that print the following patterns in four separate programs:
    Pattern I       Pattern II     Pattern III       Pattern IV
    1               1 2 3 4 5 6              1              1 2 3 4 5 6
    1 2             1 2 3 4 5              2 1                1 2 3 4 5
    1 2 3           1 2 3 4              3 2 1                  1 2 3 4
    1 2 3 4         1 2 3              4 3 2 1                     1 2 3
    1 2 3 4 5       1 2              5 4 3 2 1                       1 2
    1 2 3 4 5 6     1              6 5 4 3 2 1                         1

    Pattern 3
    public class num2
    {
    public static void main(String[] args)
    {

    int x,i,j,k;
    for(i=0;i<6;i++)
    {
    x=6;
    for(j=i+1;j<6;j++)
    {
    x--;
    System.out.print(" ");
    }
    for(k=0;k<=i;k++)
    {
    System.out.print(x--);
    }
    System.out.println();
    }

    }
    }

Similar Threads

  1. Help with Nested Loops
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 23rd, 2010, 04:31 PM
  2. Nested If Else Statement
    By jwill22 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 6th, 2010, 03:46 PM
  3. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 11:05 AM
  4. Nested try blocks
    By Ahmed. in forum Member Introductions
    Replies: 2
    Last Post: April 30th, 2010, 09:12 AM
  5. how do i draw a shape with nested loops?
    By Kilowog in forum Loops & Control Statements
    Replies: 1
    Last Post: September 25th, 2009, 01:14 AM