Hello everyone..!! Welcome to flower brackets blog. In this post we are going to learn pascal’s triangle java.

First let us understand what is pascal’s triangle, it’s logic and how it is used in java program??

#### Pascal’s triangle logic

Ok, let us understand the logic of printing pascal triangle. Pascal triangle will print numbers according to a particular order.

So printing numbers always depends on number of lines as an input. Suppose, if you take four (or more) number of lines as output for pascal triangle it looks like above figure.

In the first line it starts printing from 1. But in floyd’s triangle it starts with 2 and then 3. But in pascal triangle every line it starts with 1.

Here you don’t have any numbers so in second line it will print 1 and 1. So it starts with 1 it ends with 1.

And then in the third line it has to print three numbers, it starts with one it ends with one. But what is the middle number is the sum of 1 + 1 in the second line (see above figure).

So now in the fourth line that should be 4 numbers. It starts with 1 it ends with 1 and then the two numbers are going to be the sum of 1 + 2 and 2 + 1 that is 3 and 3 (see above figure).

So if you are asked to print 5 lines/rows then again it starts with 1 and ends with 1 and in between it will be the sum of 1 + 3, 3 + 3 and 3 + 1, that is 4, 6 and 4.

Like that pascal triangle will go on based on the number of lines as an input.

So unlike floyd’s triangle we are not incrementing the number. Everytime we are printing a number which is first initialized to 1.

But how many number of times you want to print again will depend upon the number of lines and how you control that.

Again we require one variable to take the number of lines, in this case

```
number = 4 // depends on how many lines/rows you want to print
```

And then we require two variables,

```
i = 1 // here we are iterating from 1 to number of lines/rows, that is, 4
j = 1; j <= i // j runs within the nested for loop of i
```

Here “j” is checked with the condition where “j” is less than or equal to “i” and is going to be incremented every time.

When you are printing number in every nested for loop the variable number has to be initialized to 1.

So unlike in floyd’s triangle we keep on incrementing variable “number”. But here when it enters into the nested for loop the variable number has to be initialized to 1.

Instead of,

i = 1 j = 1 initialize, i = 0 j = 0

So now after printing variable “number” we do this

```
number = number * (i - j) / (j + 1) // here we are calculating second row
```

Then we print our “number”,

System.out.print(number);

Now let’s understand this formula by writing a code,

import java.util.Scanner; public class PascalsTriangle { public static void main(String[] args) { System.out.println("Please number of lines : "); 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:**

Please number of lines : 4

1

1 1

1 2 1

1 3 3 1

#### What is PASCAL’S triangle?

Pascal triangle is a triangle of numbers 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 |

As you can see in the above triangle, the triangle has five rows and the first row starts with digit 1.

Then each digit in a row is the sum of left digit and right digit on 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.

#### Pascal’s triangle java using loop

Here we are going to learn pascal’s triangle using loop,

import java.util.Scanner; public class PascalTriangle { public static void printPascalTriangle(int num) { int previousRow[]; int presentRow[] = {1};printPascalArray(presentRow); previousRow = presentRow; for(int a = 2; a <= num; a++) { presentRow = new int[a]; presentRow[0] = 1; presentRow[a - 1] = 1; for(int b = 0; b <= a - 3; b++) { presentRow[b + 1] = previousRow[b] + previousRow[b + 1]; }printPascalArray(presentRow); previousRow = presentRow; } } public static void printPascalArray(int array[]) { for(int a = 0; a < array.length; a++) { System.out.print(array[a] + " "); } System.out.println(); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Please enter number of rows : "); int number = sc.nextInt();printPascalTriangle(number); sc.close(); } }

**Output:**

Please enter number of rows : 5

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

#### Pascal’s triangle java using recursion

Now let’s learn pascal triangle recursion java. Pascal’s triangle recursion is one of the best and quickest method to program.

It’s the least amount of programming for the results. So let’s get into the code,

import java.util.Scanner; public class PascalsTriangleRecursion { public static void printPascal(int num) { for(int a = 0; a < num; a++) { for(int b = 0; b <= a; b++) { System.out.print(pascalTriangle(a, b) + " "); } System.out.println(); } } public static int pascalTriangle(int a, int b) { if(b == 0) { return 1; } else if(b == a) { return 1; } else { returnpascalTriangle(a - 1, b - 1) +pascalTriangle(a - 1, b); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Please enter number of rows : "); int number = sc.nextInt();printPascal(number); sc.close(); } }

**Output:**

Please enter number of rows : 6

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

#### Pascal’s triangle java using binomial coefficients

Now we gonna be programming pascal’s triangle in java using a different method called binomial coefficients.

import java.util.Scanner; public class PascalTriangleJava { public static void printPascalBinomial(int num) { for(int x = 0; x < num; x++) { for(int y = 0; y <= x; y++) { System.out.print(nCk(x, y) + " "); } System.out.println(); } } public static int nCk(int x, int y) { int numerator =calculate(x); int denominator =calculate(y) *calculate(x - y); int result = (int) (numerator / denominator); return result; } public static int calculate(int num) { int result = 1; for(int i = 1; i <= num; i++) { result = result * i; } return result; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter number of rows : "); int row = sc.nextInt();printPascalBinomial(row); sc.close(); } }

**Output:**

Please enter number of rows :

10

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 7 21 35 35 21 7 1

1 8 28 56 70 56 28 8 1

1 9 36 84 126 126 84 36 9 1

##### conclusion

That’s it guys. This was all about pascal’s triangle. I hope you guys have understood the pascal’s triangle concept.

You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.