Pascal Triangle in Java
Java program to print Pascals Triangle using arrays or combinations.
Last updated: 11 January 2026
Method 1: Using Binomial Coefficient
Calculate C(n,k).
Main.javaRun in Compiler →
public class PascalTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = 0; i < rows; i++) {
int number = 1;
System.out.format("%" + (rows - i) * 2 + "s", ""); // Spacing
for (int j = 0; j <= i; j++) {
System.out.format("%4d", number);
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
}Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1Explanation
Each number is derived from the previous one: number = number * (i - j) / (j + 1)
Frequently Asked Questions
Try This Program
Copy this code and run it in our free online Java compiler.
Open Java Compiler