# Thread: Nested loops, and backwards patterns

1. Member
Join Date
Mar 2012
Posts
1
Rep Power
0

## Nested loops, and backwards patterns

I have an assignment to to print wither 1 & 4 or 2 & 3 these patterns. I chose 1 & 4

Java Code:
```#1:

1

1  2

1  2  3

1  2  3  4

1  2  3  4  5

1  2  3  4  5  6

#2:

1  2  3  4  5  6

1  2  3  4  5

1  2  3  4

1  2  3

1  2

1

#3:

1

1  2

1  2  3

1  2  3  4

1  2  3  4  5

1  2  3  4  5  6

#4:

1  2  3  4  5  6

1  2  3  4  5

1  2  3  4

1  2  3

1  2

1```
Here is my code so far, and I can't for life of me get the last to print right. Please don't tell me the answer, help me understand what I am doing wrong.

Java Code:
```// Print Pattern 1 title
System.out.println("\n#1 \n");

// Set Size of pattern
int lines = 6;

//Nested For loop
for (int row = 1; row <= lines; row++){
for (int col = 1; col <= row; col++)
System.out.print (col + "  " );
System.out.println();
System.out.println();
}

//Print Pattern 4 title
System.out.println("\n#4 \n");

//Loops for Pattern 4
for (int row = lines; row >= (lines - 6 + 1); row--){
System.out.print(" ");
}
for (int col = 1; col <= lines; col++){
System.out.print(col +  "  ");
}
System.out.println();
}
}```
Thanks!
Last edited by Evildoer; 03-19-2012 at 01:24 AM.

2. ## Re: Nested loops, and backwards patterns

Look at the desired output line by line. Then look at what is printed on a line and see what the relationship is between the number/position of the line and the number of spaces printed on the line and the number of numbers.

3. Senior Member
Join Date
Jan 2011
Location
Posts
311
Rep Power
6

## Re: Nested loops, and backwards patterns

I can see you understand the problem related to 4th pattern. You need sufficient number of blank spaces at the begining of row, where necesarry, and then correct number of numbers at the end.

Let's take a look at the part where you want to print blank spaces:

Java Code:
```//Loops for Pattern 4
for (int row = lines; row >= (lines - 6 + 1); row--){
System.out.print(" ");
}```
Here you have variable row and in this case it's value is value of lines which is 6.

Java Code:
`int row = lines;  // row = 6`
The next part will always give 1:

Java Code:
`(lines - 6 + 1);  // (6 - 6 + 1) = 1`
So basicly, you have a for loop which will print blank spaces while counting down from 6 to 1. Result: six blank spaces in a row.

For the second loop it does nothing else but print numbers from 1 to 6 with blank spaces between:

Java Code:
```for (int col = 1; col <= lines; col++){
System.out.print(col +  "  ");
}```
Everything is printed in one line since you used row to represent blank spaces instead of lines i.e. there is no for loop that counts and breaks lines.

So let's remeber how it should looks like:

Java Code:
```1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1```
Can you notice this pattern: row 1 - blanks 0; row 2 - blanks 1; row 3 - blanks 2; row 4 - blanks 3 ...

What you need here is one variable for counting lines (row),
one for counting columns (col),
and I suggest one for counting blank spaces (space).

Your code could look like this:

Java Code:
```for (each line)  // rows from 1 to 6
for (each space necessary)  // hint: remember the pattern, loop space from 1 and if it is lower then
// current row you have to print space
print blank space
for (rest number of columns)  // count how meny columns are left
print number accordingly
print new line```
I suppose your requirement is to use only for loops so this is only one of possible sollutions.
Last edited by milovan; 03-19-2012 at 05:59 AM.

4. Member
Join Date
Nov 2012
Posts
4
Rep Power
0

## Re: Nested loops, and backwards patterns

Hello every one,

I got the same problem as Evildoer, especialy number 4. Could anyone just give the answer? I had sit for more than two hours to solve it :(, but I guess I have to give up my self now..Thanks in advance!

5. ## Re: Nested loops, and backwards patterns

I got the same problem as Evildoer, especialy number 4. Could anyone just give the answer?
Seriously? First off, don't hijack someone else's thread. Secondly, we're not your personal code farm. If you expect to be handed answers as easy as this one for the rest of your programming career, give up now and stop wasting everyone's time.

6. ## Re: Nested loops, and backwards patterns

Enough with meaninglessly bringing this dead thread to the top of the listings. jou, please go through Guide For New Members and BB Code List - Java Programming Forum .

db