Pascal’s triangle java

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

Pascal's Triangle Java

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 logic

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 five 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? It’s the sum of 1 + 1 in the second line (see above figure).

So now in the fourth line there should be 4 numbers. Again it starts with 1 and ends with 1, 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 “i” 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 variable “number”,

System.out.print(number);

Java program to print pascal triangle

import java.util.Scanner;

public class PascalsTriangle 
{
   public static void main(String[] args) 
   {
      System.out.println("Please enter 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 enter number of lines : 4
1
1   1
1   2   1
1  3   3   1

Related Posts