Java program to display pascal triangle

Let’s learn java program to display pascal triangle.

Java program to display pascal triangle

java program to display pascal triangle

Pascal triangle program in java is one of the common java interview question.

Also read – garbage collection in java

Pascal’s triangle is a triangle of binomial coefficients arranged in the form of a triangle. Pascal’s triangle is named after french mathematician Blaise Pascal.

Pascal’s triangle looks like this,

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

In the above pascal’s 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.https://www.java.com/en/

Here’s the java program to print pascal’s triangle,

import java.util.Scanner;

public class PascalsTriangleJava 
{
   static int findFactorial(int number)
   {
      int factorial;
      for(factorial = 1; number > 1; number--)
      {
         factorial *= number;
      }
      return factorial;
   }

   // here's the function to display pascal's triangle
   static int printPascalTraingle(int num, int p) 
   {
      return findFactorial(num) / ( findFactorial(num - p) * findFactorial(p));
   }

   public static void main(String[] args) 
   {
      int row, a, b;
      System.out.println("Please enter number of rows: ");
      Scanner sc = new Scanner(System.in);
      row = sc.nextInt();
      System.out.println("Here's is pascal's triangle: ");
      for(a = 0; a < row; a++) 
      {
         for(b = 0; b < row - a; b++)
         {
            System.out.print(" ");
         }
         for(b = 0; b <= a; b++)
         {
            System.out.print(" " + printPascalTraingle(a, b));
         }
         System.out.println();
      }
      sc.close();
   }
}



Output:

Please enter number of rows: 6
Here’s is pascal’s triangle:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Please enter number of rows: 9
Here’s is pascal’s 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 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1


Without using arrays

Now let’s learn pascal triangle program in java without using arrays. To write a program to print pascal triangle without using array we are using two loops.

Outer loop print number of rows and inner loop prints numbers in each rows. Here’s java program to print pascal’s triangle for given number of rows,

import java.util.Scanner;

public class PascalTriangleDemo 
{
   public static void main(String[] args) 
   {
      System.out.println("Please enter number of rows to print pascal's triangle: ");
      Scanner sc = new Scanner(System.in);
      int row = sc.nextInt();            
      System.out.printf("Pascal's triangle with %d rows %n", row);
      displayPascalTriangle(row);
      sc.close();
   }

   public static void displayPascalTriangle(int r) 
   {
      for(int a = 0; a < r; a++) 
      {
         int num = 1;
         System.out.printf("%" + (r - a) * 2 + "s", "");
         for(int b = 0; b <= a; b++) 
         {
            System.out.printf("%4d", num);
            num = num * (a - b) / (b + 1);
         }
         System.out.println();
      }
   }
}



Output:

Please enter number of rows to print pascal’s triangle: 5
Pascal’s triangle with 5 rows
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

Please enter number of rows to print pascal’s triangle: 7
Pascal’s triangle with 7 rows
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

Using array

Here we are going to learn pascal triangle in java using array. To write pascal triangle code in java using arrays we have to use an array with two extra columns.

Let’s see java program to display pascal triangle,

public class PascalTriangleUsingArray 
{
   public static void main(String[] args) 
   {
      printPascalTriangle(8); // calling function
   }

   public static void printPascalTriangle(int num)
   {   
      // if there is any invalid input
      if(num == 0)
      {
         return;
      }
      // input number of rows
      int rows = num;
      // adding two extra columns
      int column = 2 * num + 1;
      int[][] arrPascal = new int[rows][column];
      // starting from 1 in first row
      arrPascal[0][num] = 1;
      // here we are looping 
      // through each position     
      for(int a = 1; a < rows; a++)
      {                                        
         for(int b = 1; b < column - 1; b++)
         {                     
            arrPascal[a][b] = arrPascal[a - 1][b - 1] + arrPascal[a - 1][b + 1];
         }
      }
      // loop for printing
      // when they're not 0.    
      for(int a = 0; a < rows; a++)
      {
         for(int b = 1; b < column-1; b++)
         {
            if(arrPascal[a][b] != 0)
            {
               System.out.print(arrPascal[a][b]);
            }
            else
            {
               System.out.print(" ");
            }
         }
         System.out.println();
      }
      return;
   }
}



Output:

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

Pascal triangle recursion

Here let’s learn pascal’s triangle recursion java. Let’s see pascal triangle code,

import java.util.Scanner;

public class RecursionPascalTriangle 
{
   public static void display(int num)
   {
      for(int a = 0; a < num; a++)
      {
         for(int b = 0; b <= a; b++)
         {
            System.out.println(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
      {
         return pascalTriangle(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 row = sc.nextInt();
      display(row);
      sc.close();
   }
}



Output:

Please enter number of rows: 8

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