0
0
JavaProgramBeginner · 2 min read

Java Program to Print Pascal Triangle

You can print Pascal's Triangle in Java by using nested loops and calculating each value with the formula value = value * (row - col) / (col + 1) inside the loops to generate each row.
📋

Examples

Input5
Output1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
Input1
Output1
Input0
Output
🧠

How to Think About It

To print Pascal's Triangle, think of each row as a list of numbers where each number is the sum of the two numbers directly above it in the previous row. Start with 1 at the top, then for each new row, calculate values using the previous row's values or use a formula to find each number directly.
📐

Algorithm

1
Get the number of rows to print.
2
For each row from 0 to number of rows - 1:
3
Start with value = 1.
4
For each position in the row from 0 to current row number:
5
Print the current value.
6
Update the value using value = value * (row - col) / (col + 1).
7
Move to the next line after printing all values in the row.
💻

Code

java
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();
        }
    }
}
Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
🔍

Dry Run

Let's trace printing 3 rows of Pascal's Triangle through the code.

1

Row 0 start

i=0, value=1

2

Print first value

Print 1, update value = 1 * (0 - 0) / (0 + 1) = 0

3

End row 0

Print newline

4

Row 1 start

i=1, value=1

5

Print first value

Print 1, update value = 1 * (1 - 0) / (0 + 1) = 1

6

Print second value

Print 1, update value = 1 * (1 - 1) / (1 + 1) = 0

7

End row 1

Print newline

8

Row 2 start

i=2, value=1

9

Print first value

Print 1, update value = 1 * (2 - 0) / (0 + 1) = 2

10

Print second value

Print 2, update value = 2 * (2 - 1) / (1 + 1) = 1

11

Print third value

Print 1, update value = 1 * (2 - 2) / (2 + 1) = 0

12

End row 2

Print newline

Row iCol jValue printedValue updated
0010
1011
1110
2012
2121
2210
💡

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

Using 2D array to store values
java
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();
        }
    }
}
This approach uses extra memory to store the triangle but makes it easy to access any value later.
Recursive method to calculate values
java
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();
        }
    }
}
This recursive method is simple but inefficient for large rows due to repeated calculations.

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.

ApproachTimeSpaceBest For
Formula in loopsO(n^2)O(1)Fast printing with minimal memory
2D array storageO(n^2)O(n^2)Accessing any value later
Recursive calculationExponentialO(n)Simple code but inefficient
💡
Use the formula value = value * (row - col) / (col + 1) inside loops to efficiently generate Pascal's Triangle without extra storage.
⚠️
Beginners often forget to update the value inside the inner loop correctly, causing wrong numbers or infinite loops.