# Creating number Pyramids with For Loops

• October 2nd, 2011, 04:23 PM
Staticity
Creating number Pyramids with For Loops
So, I just had a random urge (after reading a previous post) to make an infinite loop that creates this output:

1
12
123
1234
12345
1234
123
12
1
12
123
.... etc.

So far I have this:

Code :

```import java.util.Scanner;   public class ForTriangle { public static void main(String args[]){   Scanner scan = new Scanner(System.in); System.out.println("Enter length of numbers"); int number = scan.nextInt();   int a = 1; int b = 1;   while(b < number){ for(a = 1; a <= number; a++){ for(b = 1; b <= a; b++){ if(b == number) a = 0; System.out.print(b); } System.out.println(); }   }   }//End MM }//End MC```

When the user enters 5, it produces:

1
12
123
1234
12345
1
12
123
1234
12345
etc...

Any tips?
• October 2nd, 2011, 04:53 PM
Sean4u
Re: Creating number Pyramids with For Loops
Quote:

Any tips?
You can look at it as two sequences, one like this:
Quote:

1
12
123
1234
and one like this
Quote:

12345
1234
123
12
A loop that does those two over and over could be about right
• October 2nd, 2011, 05:13 PM
Staticity
Re: Creating number Pyramids with For Loops
If I try to do this, the first for loop will run on forever, and ever.. And the second for loop will never be used because the first for loop never ended. I already seemed to have lost interest in this.. Thanks for replying so quickly though Sean!
• October 2nd, 2011, 05:20 PM
Sean4u
Re: Creating number Pyramids with For Loops
You need an outer 'loop forever' with two inner loops. Inside the outer loop the first inner loop loops from min to max - 1, then it exits. The second inner loop loops from max to min + 1, then exits. After the second inner loop exits, the outer 'loop forever' makes the first loop run again...