I am trying to draw pascal triangle in java
Here is my code
static void printPascalTriangle(int length){
int[][] pascal=new int[length][length];
for (int i = 0; i < length; i++) {
pascal[0][i]=1;
pascal[i][0]=1;
} // here length is the dimension of pascal triangle , first loop is for filling up the first row and column with zero
for (int row = 1; row < length; row++) {
for (int column = 1; column < length-row; column++) {
pascal[row][column]=pascal[row][column-1]+pascal[row-1][column];
}
} // this loop is for filling up the rest of position
for (int row = 0; row < length; row++) {
for (int column = 0; column < length-row; column++) {
pr(pascal[row][column]+"\t");
}
prln("\n");
} // this loop is for printing the array
}
static void prln(Object anyObject){
System.out.println(anyObject);
}
static void pr(Object anyObject){
System.out.print(anyObject);
}
I have used three loops here , How can I solve this problem by using a single loop , or at least two loop , I do not want to use a different loop for printing the array .