## Java program to print Pascal triangle

Write a **Java program to print Pascal triangle** is one of the frequently asked Java coding interview questions. The question tests the candidate’s ability to write logic and the knowledge of using nested for loops.

### Pascal triangle

Pascal’s triangle is a set of numbers arranged in the form of a triangle. Let us understand it better with the help of a diagram. Below is the pattern of pascal’s triangle.

Following are the characteristics of Pascal’s triangle

- The number of elements in a row is equal to the row
^{th}number i.e. first row contains one element, second row contains two elements, third row contains three elements and so on. - Each row of a Pascal’s Triangle can be calculated from the previous row. Each row begins and ends with the number 1. The remaining numbers in a row is calculated from the sum of the left number and right number on the previous row. For example, the second element in the 3rd row is the sum of the 1st element and the 2nd element of the 2nd row. Similarly the third element in the 4th row is the sum of the 2nd element and the 3rd element of 3rd row. Similar concept applies for other elements.
- The sum of numbers in each row is twice the sum of numbers in the previous row.
- The diagonals adjacent to the border diagonals contains natural numbers in order.

Now lets see the Java program to print pascal triangle.

### Java program to print pascal triangle using recursion

This problem can also be solved using recursion since calculating the value of a particular element in the triangle depends on the previously calculated values.

The number at position row i and column j can be represented as pascal(i,j) with i and j starting from 0. The first and last element in a row is always 1 hence we can write as,

**pascal ( i, 0 ) = 1**

**pascal ( i, i ) = 1**

Following is the recursive formula used to calculate the remaining elements :

**pascal ( i, j ) = pascal ( i – 1 , j -1 ) + pascal ( i – 1 , j )** // refer point 2 in the characteristics for an understanding.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import java.util.Scanner; public class PascalTriangle { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the number of rows to print: "); int rows = scanner.nextInt(); System.out.println("Pascal Triangle:"); print(rows); scanner.close(); } public static void print(int n) { for (int i = 0; i < n; i++) { for (int k = 0; k < n - i; k++) { System.out.print(" "); // print space for triangle like structure } for (int j = 0; j <= i; j++) { System.out.print(pascal(i, j) + " "); } System.out.println(); } } public static int pascal(int i, int j) { if (j == 0 || j == i) { return 1; } else { return pascal(i - 1, j - 1) + pascal(i - 1, j); } } } |

1 2 3 4 5 6 7 8 9 | Enter the number of rows to print: 7 Pascal Triangle: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 |

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.Scanner; public class PascalTriangle { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("Enter number of rows to print:"); int rows = s.nextInt(); System.out.println("Pascal Triangle:"); for (int i = 0; i < rows; i++) { for (int k = 0; k < rows - i; k++) { System.out.print(" "); } int number = 1; for (int j = 0; j <= i; j++) { System.out.print(number + " "); number = number * (i - j) / (j + 1); } System.out.println(); } s.close(); } } |

1 2 3 4 5 6 7 8 9 | Enter number of rows to print: 6 Pascal Triangle: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 |

Hope you find the post useful. For any comments, use the comments section.

Also read my another post,

Java program to print floyd’s triangle

#### Latest posts by Uday (see all)

- How to run shell commands using SSH connection in Java - July 26, 2017
- Java program to print rectangle of stars - July 20, 2017
- Introduction to Java Enum data type with example programs - March 31, 2017