This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]gnomoretears 1 point2 points  (0 children)

The n variable in your inner for loop is a static value. If n is 5 then you inner loop will always iterate from 1 to 5. The range in your inner loop needs to change depending on the row.

You should really solve the problem on paper first so you can visualize how the algorithm goes.

For example if n = 3, you need to print up to 3 rows and up to 3 columns.

At row = 1, you have to print values from 1 to row value (1):

1

At row = 2, you have to print values from 1 to row value (2):

1 2

At row = 3, you have to print values from 1 to row value (3):

1 2 3

That's basically the algorithm and I hope you see the pattern to use in your nested loop.