I need a code for making a Pascal's triangle. This code is for a Right triangle, but I need it to be a Pascal's triangle. It needs to have 10 rows and does have a gap in the middle of the Top and the Bottom.
Can anyone please help me on this? for
loops will be fine.
public static int get_pascal(int row, int col) {
if (col == 0 || col == row) {
return 1;
} else {
return get_pascal(row - 1, col - 1) + get_pascal(row - 1, col);
}
}
public static void main(String[] args) {
//row size variable
int rowNum = 5;
levels = new String[rowNum];
int i = 0;
int arIndex = 0;
System.out.println(recurseRow(i, rowNum, arIndex));
System.out.println(" ");
System.out.println(upsideDown(rowNum - 1));
}
//Recursion for row
public static String recurseRow(int i, int rowNum, int arrayIndex) {
if (i == rowNum)
return "";
else {
int k = 0;
int next = i + 1;
String str = recurseCol(i, k);
levels[arrayIndex] = str;
arrayIndex += 1;
return str + "\n" + recurseRow(next, rowNum, arrayIndex);
}
}
//Recursion for column
public static String recurseCol(int i, int k) {
if (k > i)
return "";
else {
int next = k + 1;
return get_pascal(i, k) + " " + recurseCol(i, next);
}
}
//upside down recursion
public static String upsideDown(int index) {
if (index < 0) {
return "";
} else {
String str = levels[index];
index -= 1;
return str + "\n" + upsideDown(index);
}
}