Let’s learn java program to display pascal triangle.

## Java program to display pascal triangle

Pascal triangle is a triangle of binomial coefficients arranged in the form of a triangle. It is named after french mathematician Blaise Pascal. Pascal triangle looks like this,

1 | ||||||||

1 | 1 | |||||||

1 | 2 | 1 | ||||||

1 | 3 | 3 | 1 | |||||

1 | 4 | 6 | 4 | 1 |

In the above triangle there are five rows. First row starts with digit 1. Then each digit in a row is the sum of left digit and right digit of above row. Suppose, if a digit is missing in the above row, it is assumed as 0.

**NOTE:**

- diagonals adjacent to the border diagonals contain natural numbers in an order.
- sum of numbers in each row is twice the sum of numbers in above row.

Binomial coefficient can be calculated using formula,

C(n,r) = n!/((n-r)!*r!)

**Time complexity:** O(n²) where n is number of lines.

Here’s the java program to print pascal triangle,

import java.util.Scanner; public class PascalsTriangleJava { public static void main(String[] args) { System.out.println("Please enter number of rows : "); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); int row = num; int number = 1; for(int i = 0; i < num; i++) { for(int r = 1; r <= row; r++) { System.out.print(" "); } number = 1; for(int j = 0; j <= i; j++) { System.out.print(number + " "); number = number * (i - j) / (j + 1); } row--; System.out.println(); sc.close(); } } }

**Output:**

Also read – garbage collection in java