Java program to display pascal triangle

Let’s learn 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.

java program to display pascal triangle

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!)

Also read – garbage collection in java

Time complexity: O(n²) where n is number of lines.

Here’s the java program to display pascal 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


Pascal triangle 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 for loops.

Outer for loop print number of rows and inner for 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.println("Pascal's triangle with " + row + " rows.");
      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


pascal triangle in java 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 two dimensional array.

Let’s see java program to display pascal triangle or pascal triangle in java using array.

import java.util.Scanner;
public class PascalTriangleUsingArray
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      int num, a, b, arr[][], p;
      System.out.println("Please enter number of rows: ");
      num = sc.nextInt();
      p = num - 1;
      arr = new int[num][num];
      for(a = 0; a < num; a++)
      {
         for(b = 0; b <= a; b++)
            if(b == 0 || b == a)
               arr[a][b] = 1;
            else
               arr[a][b] = arr[a - 1][b - 1] + arr[a - 1][b];
      }
      System.out.println("Pascal's triangle: \n");
      for(a = 0; a < num; a++)
      {
         for(b = 0; b <= p; b++)
            System.out.print(" ");
         p--;
         for(b = 0; b <= a; b++)
            System.out.print(arr[a][b] + " ");
         System.out.println();
      }
      sc.close();
   }
}


Output:

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


Pascal triangle recursion java

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 || 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.println("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