Skip to content
Advertisement

Pascal’s triangle 2d array – formatting printed output

I have a small assignment where I have to use a 2d array to produce Pascal’s triangle. Here is my code, and it works. There is an extra credit opportunity if I display the triangle like so:

Pascal's triangle

However, my spacing is not formatted like that. it simply displays the numbers all lined up on the left. its hard to describe but if you run it you will see what I mean.

Here is my code:

JavaScript

If someone could help me figure out how to add the correct spacing to my program to produce the output desired in the picture, that would be great. I know I need to put a System.out.print(" ") somewhere. I just dont know where.

Advertisement

Answer

Here I had modified your code, it prints wonderfully for ROW size till 13, because of the limitation of my console window:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement