help with a loop (pyramid of numbers)

i have been working on this program for like 4 hours and i just cannot come up with a solution...i do know that loops have to be used....i just having way to much problems

Problem:

Write a program that prompts the user to enter an intger from 1 to 15 and displays a pyramid, as shown in the following sample run:

Enter the number of lines: 7

______1

_____212

____32123

___4321234

__543212345

_65432123456

7654321234567

those are suppose to be spaces instead of underscores...its only way i could make it look right on this post

Re: help with a loop (pyramid of numbers)

What have you come up with as a design so far? The program needs to use nested loops. Outer for the rows and inner for each row.

Re: help with a loop (pyramid of numbers)

Ok, well provide us with the code you have first, and then we can help you more from there.

Lets have a look at the design of a pyramid. First of all, since this is a 2 dimensional image (vertical and horizontal), we need a nested loop. A nested loop is a loop inside a loop. For example, the following code:

Code java:

for(int i=0;i<5;i++)
{
for(int x=0;x<5;x++)
{
System.out.println("Loop");
}
}

will print out the word: **Loop** 25 times. The reason is because it will print it out 5 times for every loop of the outer loop. 5 x 5 is equal to 25, so it will run the inner loop a total of 25 times.

Now, since we are making a pyramid we want the limit of our inner loop to be dynamic. Specifically, after row 1, each row has 2 more numbers than the preceding row. So, we want to probably base our inner loop condition off of our outer loop count variable.

There is another thing to consider. How many spaces do we need before the first number on each line? Well, the design of a pyramid says the base is twice as wide as the height. But, we want our first number to be in the center of the pyramid, so that number will be half of the base, which is conveniently equal to height. That is for the first row. Continuing the design of the pyramid tells us the number of spaces before a number on any give line is 1 less than the number of spaces before the number of the preceding line.

The last thing is the numbers. Notice how the first number on each row is equal to the number of the row in respect to the top, where: 1 is the top and the last row is the height.

See if you can work with those instructions.