Step through the logic of your program using pen and paper.
For your “horizontal” loop, you go all the way up to n
each time. Is that right? I think you meant to go only as far as x
, as this is the value that increases with each line.
The other problem is that you have one too many of everything, because you used <=
rather than <
.