Java Program to Print Pascal Triangle
value = value * (row - col) / (col + 1) inside the loops to generate each row.Examples
How to Think About It
Algorithm
Code
public class PascalTriangle { public static void main(String[] args) { int rows = 5; for (int i = 0; i < rows; i++) { int value = 1; for (int j = 0; j <= i; j++) { System.out.print(value + " "); value = value * (i - j) / (j + 1); } System.out.println(); } } }
Dry Run
Let's trace printing 3 rows of Pascal's Triangle through the code.
Row 0 start
i=0, value=1
Print first value
Print 1, update value = 1 * (0 - 0) / (0 + 1) = 0
End row 0
Print newline
Row 1 start
i=1, value=1
Print first value
Print 1, update value = 1 * (1 - 0) / (0 + 1) = 1
Print second value
Print 1, update value = 1 * (1 - 1) / (1 + 1) = 0
End row 1
Print newline
Row 2 start
i=2, value=1
Print first value
Print 1, update value = 1 * (2 - 0) / (0 + 1) = 2
Print second value
Print 2, update value = 2 * (2 - 1) / (1 + 1) = 1
Print third value
Print 1, update value = 1 * (2 - 2) / (2 + 1) = 0
End row 2
Print newline
| Row i | Col j | Value printed | Value updated |
|---|---|---|---|
| 0 | 0 | 1 | 0 |
| 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 0 |
| 2 | 0 | 1 | 2 |
| 2 | 1 | 2 | 1 |
| 2 | 2 | 1 | 0 |
Why This Works
Step 1: Start with 1
Each row starts with the number 1 because Pascal's Triangle always begins rows with 1.
Step 2: Calculate next values
Each next value in the row is calculated using the formula value = value * (row - col) / (col + 1) which uses the previous value to find the next.
Step 3: Print row values
Print all values in the current row separated by spaces, then move to the next line for the next row.
Alternative Approaches
public class PascalTriangle { public static void main(String[] args) { int rows = 5; int[][] triangle = new int[rows][]; for (int i = 0; i < rows; i++) { triangle[i] = new int[i + 1]; triangle[i][0] = 1; triangle[i][i] = 1; for (int j = 1; j < i; j++) { triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j]; } } for (int i = 0; i < rows; i++) { for (int j = 0; j <= i; j++) { System.out.print(triangle[i][j] + " "); } System.out.println(); } } }
public class PascalTriangle { public static int pascal(int row, int col) { if (col == 0 || col == row) return 1; return pascal(row - 1, col - 1) + pascal(row - 1, col); } public static void main(String[] args) { int rows = 5; for (int i = 0; i < rows; i++) { for (int j = 0; j <= i; j++) { System.out.print(pascal(i, j) + " "); } System.out.println(); } } }
Complexity: O(n^2) time, O(1) space
Time Complexity
The program uses nested loops where the outer loop runs n times and the inner loop runs up to n times, resulting in O(n^2) time.
Space Complexity
The main approach uses only a few variables and prints values directly, so it uses O(1) extra space.
Which Approach is Fastest?
The formula-based approach is fastest and uses least memory, while recursive approach is slow and 2D array uses more memory but allows easy access to all values.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Formula in loops | O(n^2) | O(1) | Fast printing with minimal memory |
| 2D array storage | O(n^2) | O(n^2) | Accessing any value later |
| Recursive calculation | Exponential | O(n) | Simple code but inefficient |
value = value * (row - col) / (col + 1) inside loops to efficiently generate Pascal's Triangle without extra storage.