Help with Pascal's triangle
Hi All,
I am creating a Pascal's Triangle:
Wherein the output should be
Output:
1
11
121
1331
14641
I have written a code to do this but my output is not perfect.
My Code:
publicclass Pascal
{
publicstaticvoid main(String[] args)
{
int[][] tri =newint[5][];
for (int r = 0; r < tri.length; r++)
{
tri[r] =newint[r + 1];
tri[r][0] = 1;
tri[r][r] = 1;
for (int c = 1; c < r; c++)
{
tri[r][c] = tri[r - 1][c] + tri[r - 1][c - 1];
}
}
for (int r = 0; r < tri.length; r++)
{
for (int c = 0; c < tri[r].length; c++)
{
System.out.print(" " + tri[r][c]);
}
System.out.println("");
}
}
}
The Output I am getting looks like this:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Please Help...
Desired output:
1
11
121
1331
14641

