Pascal Triangle in Java

Pascal Triangle in Java | Pascal triangle is a triangular array of binomial coefficients. In pascal’s triangle, each number is the sum of the two numbers directly above it.

     1 
    1 1 
   1 2 1 
  1 3 3 1 
 1 4 6 4 1 

Pascal Triangle Program in Java Without Using an Array

After observation, we can conclude that the pascal always starts with 1 and next digits in the given row can be calculated as, number = previous_number_in_row * (row–column) / column

import java.util.Scanner;

public class PascalTriangle {

   public static void main(String[] args) {
      // take input
      Scanner scan = new Scanner(System.in);
      int n = 0;
      System.out.print("Enter Number of Rows:: ");
      n = scan.nextInt();
      displayPascalTriangle(n);
      scan.close();
   }

   private static void displayPascalTriangle(int n) {
      // temp variable
      int a = 0;

      // for negative numbers
      if(n <= 0)
      System.out.println("Enter Positive Number");

      for(int i=1; i <= n; i++) {
         // in each iteration start with 1
         a = 1;

         // print number
         for(int j = 1; j <= i; j++) {
            System.out.print(a + " ");
            // update variable
            a = a * (i-j) / j;
         }

         // new line
         System.out.println();
      }
   }
}

Output for different values:-

Enter Number of Rows:: 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Enter Number of Rows:: 7
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

Pascal Triangle in Java at the Center of the Screen

We can display the pascal triangle at the center of the screen. For this, just add the spaces before displaying every row. Generally, on a computer screen, we can display a maximum of 80 characters horizontally. Half of 80 is 40, so 40th place is the center of the line.

private static void displayPascalTriangle(int n) {
   // temp variable
   int a = 0;

   // for negative numbers
   if(n <= 0)
   System.out.println("Enter Positive Number");

   for(int i=1; i <= n; i++) {
      // in each iteration start with 1
      a = 1;

      // add space
      for(int s=1; s <= (40 - i); s++)
      System.out.print(" ");

      // print number
      for(int j = 1; j <= i; j++) {
         System.out.print(a + " ");
         // update variable
         a = a * (i-j) / j;
      }

      // new line
      System.out.println();
   }
}

Output:-

Enter Number of Rows:: 5
     1 
    1 1 
   1 2 1 
  1 3 3 1 
 1 4 6 4 1 
Enter Number of Rows:: 7
       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

Pascal Triangle in Java using Two-dimensional Array

Using Java two-dimensional array we can find array elements as,

if(j==0 || j==i) pascal[i][j] = 1;
else
pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];

For the first and last column, the array element is 1, and for remaining elements, it is the sum of the two numbers directly above it. The java method can be written as,

private static void displayPascalTriangle(int n) {
   // declare array 
   int pascal[][] = new int[n][n];

   for(int i=0; i < n; i++) {
      
      // add space
      for(int s=1; s <= (40 - i); s++)
      System.out.print(" ");

      for(int j = 0; j <= i; j++) {
         // calculate array elements
         if(j==0 || j==i) pascal[i][j] = 1;
         else 
         pascal[i][j] = pascal[i-1][j-1] + pascal[i-1][j];

         // display array 
         System.out.print(pascal[i][j] +" ");
      }

      // new line
      System.out.println();
   }
}

Output:-

Enter Number of Rows:: 7
       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

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *