import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Pascal {
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
int[][] pascal = new int[N+1][];
// initialize first row
pascal[0] = new int[3];
pascal[0][1] = 1;
// fill in Pascal's triangle
for (int i = 1; i <= N; i++) {
pascal[i] = new int[i + 3];
for (int j = 1; j < pascal[i].length - 1; j++)
pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];
}
// print results
for (int i = 0; i <= N; i++) {
for (int j = 1; j < pascal[i].length - 1; j++) {
System.out.print(pascal[i][j] + " ");
}
System.out.println();
}
}
}
No comments:
Post a Comment