# Nested loops, and backwards patterns

• 03-19-2012, 01:20 AM
Evildoer
Nested loops, and backwards patterns
I have an assignment to to print wither 1 & 4 or 2 & 3 these patterns. I chose 1 & 4

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.

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!
• 03-19-2012, 02:14 AM
Norm
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.
• 03-19-2012, 05:41 AM
milovan
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:

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.

Code:

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

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:

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:

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:

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.
• 11-15-2012, 12:14 AM
Nazarius
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!
• 11-15-2012, 08:51 PM
Re: Nested loops, and backwards patterns
Quote:

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.
• 11-24-2012, 08:38 AM
DarrylBurke
Re: Nested loops, and backwards patterns
Enough with meaninglessly bringing this dead thread to the top of the listings. jou, please go through http://www.java-forums.org/forum-gui...w-members.html and BB Code List - Java Programming Forum .

db