Skip to content

Instantly share code, notes, and snippets.

@jukworks
Created November 2, 2012 06:07
Show Gist options
  • Save jukworks/3998991 to your computer and use it in GitHub Desktop.
Save jukworks/3998991 to your computer and use it in GitHub Desktop.
Pascal Triangle in Java
public class PascalTriangle {
public static final int LINES = 10;
public static void main(String[] args) {
int[][] a = new int[LINES][];
for (int i = 0, j = 1; i < LINES; i++, j++) {
a[i] = new int[j];
for (int k = 0; k < j; k++) {
a[i][k] = (i == 0 || k == 0 || k == j - 1) ? 1 : a[i - 1][k - 1] + a[i - 1][k];
}
}
for (int i = 0; i < LINES; i++) {
for (int j = 0; j <= i; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment