Created
November 2, 2012 06:07
-
-
Save jukworks/3998991 to your computer and use it in GitHub Desktop.
Pascal Triangle in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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