
Hi,
Cna any one please share the code to print pascal traiangle??
1 1 1 1 2 1
1 3 3 1 1 4 5 4 1
Regards: Akash

hi friend,
Here I am giving the code for printing pascal triangle in Java. Hope this will helpful for you.
public class PascalTriangleExample {
public void displayTriangle() {
int[] row = new int[0];
for (int i = 0; i < 6; i++) {
row = calculateRowValue(row);
for (int j = 0; j < 6 - i; j++) {
System.out.print(" ");
}
for (int col = 0; col < row.length; col++) {
System.out.print(row[col] + " ");
}
System.out.println();
}
}
public int[] calculateRowValue(int[] prevRow) {
int[] rowValue = new int[prevRow.length + 1];
rowValue[0] = 1;
rowValue[rowValue.length - 1] = 1;
for (int c = 1; c < rowValue.length - 1; c++) {
rowValue[c] = prevRow[c - 1] + prevRow[c];
}
return rowValue;
}
public static void main(String[] args) {
PascalTriangleExample pt = new PascalTriangleExample();
pt.displayTriangle();
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.