Berikut Source Code : SegitigaPascal.java
//----------------------
import java.util.ArrayList;
import java.util.List;
/
public class SegitigaPascal {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int n = 10;
int segitiga[][] = new int[n + 1][n + 1];
for (int i = 0; i <= n; i++) { for (int j = 0; j <= i; j++) { if (j == 0 || j == i) { segitiga[i][j] = 1; } else { segitiga[i][j] = segitiga[i - 1][j - 1] + segitiga[i - 1][j]; } } } List
for (int i = 0; i <= n; i++) {
String currentRow = "";
for (int j = 0; j <= n; j++) {
if (segitiga[i][j] != 0) {
currentRow += segitiga[i][j]+" ";
}
}
centered.add(currentRow);
}
int longestString = centered.get(n-1).length()-1;
for (int i = 0; i < n; i++) {
int longCurrentStr = centered.get(i).length()-1;
for (int j = 0; j < (longestString-longCurrentStr)/2; j++) {
System.out.print(" ");
}
System.out.println(centered.get(i));
}
}
}
Tidak ada komentar:
Posting Komentar