• November 13th, 2013, 11:23 PM
winn1me
I've been trying to write a program for the last 3 hours with no success. What probably should seem easy to most I'm having difficulty with. I have to print two separate patterns on the same program.

I've selected the following two:

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

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

Imagine the X's are not there on the one above. Every time I previewed the post it showed it a different way without the X's.

This is the two patterns stacked up, any ideas?

Here's what I have so far and it gets me to the first line of pattern 2:

public class TwoPatterns {
public static void main(String[] args) {

for (int a = 1; a <= 6; a++) {
for (int b = 1; b <= a; b++) {
System.out.printf("%-4d", b);
}
System.out.println();
}
for (int c = 6; c >= 6; c--) {
for (int d = 1; d <= c; d++) {
System.out.printf("%-4d", d);
}
System.out.println();
}
}
}

any help would be so appreciated!
• November 13th, 2013, 11:38 PM
Junky
On each line you need to print M number of spaces before you start printing the numbers. Surely you can see a pattern in how many spaces are needed.
• November 14th, 2013, 06:56 AM
winn1me
Quote:

Originally Posted by Junky
On each line you need to print M number of spaces before you start printing the numbers. Surely you can see a pattern in how many spaces are needed.

I understand that I need to probably have an if/else statement in there that displays a space (" ") if the column is less than or equal to the row but I'm not sure where to put it. I played around with a loop after the second set of nested fors but I could never get it to print what I needed, it seemed to always print on the same line as the first set.

I then tried to enter in more println but that obviously didn't do the trick, it just spaced out my program more. Do I not need the 3rd and 4th for loops? Should I be doing the if loop directly after the 2nd for loop?

--- Update ---

This is the other version I was playing with:

public class TestPatterns {
public static void main(String[] args) {

//Set how many rows you want per pattern
int column = 12;

for (int a = 1; a <= column; a++) {

for (int b = 1; b <= column; b++) {
if(b < a)
System.out.print(" ");
else
System.out.printf("%-4d", b);
}
System.out.println();
}
}
}
• November 14th, 2013, 07:04 AM
try this

Code :

```for(int i=1;i<7;i++){   for(int k=1;k<=i;k++) System.out.print(k);   System.out.println() ///// for LINE Breaking Work With Outer Loop }     second petren   for(int i=1;i<7;i++){   for(int k=1;k<i;k++) System.out.print(" "); for(int m=6;m<=i; m++) System.out.print(m)   System.out.println() ///// for LINE Breaking Work With Outer Loop }```
• November 14th, 2013, 07:09 AM
Norm
• November 14th, 2013, 09:57 AM
winn1me
Although that's helpful, Norm is right. I'm looking for the logic so I can understand how to code this in the future. I am, however; grateful for your post, unlike some that assume everyone knows.

Mike
• November 14th, 2013, 11:56 AM
copeg