So far I have this, but I am not quite sure how printPTriangle can print the triangle using the code. If someone could help me out with this, I would be grateful.
public static int factorial(int n) {
if (n == 1) {
return 1;
}
return n * (factorial(n - 1));
}
public static int pascalsNumber(int x, int y) {
return factorial(x)/(factorial(y) * factorial((x - y))); //Using combinations formula
}
public static void printPTriangle(int z) {
}
Try this,
public class PascalTriangle {
public static void main(String[] args) {
int rows = 10;
for(int i =0;i<rows;i++) {
int number = 1;
System.out.format("%"+(rows-i)*2+"s","");
for(int j=0;j<=i;j++) {
System.out.format("%4d",number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
}
Note the formatting commands used above to create a nicely formatted triangle. %4d instructs the formatter to print the number within 4 spaces.