Related Articles

Related Articles

Java Program to Print Pascal’s Triangle
  • Last Updated : 20 Nov, 2020

Pascal’s triangle is a pattern of triangle which is based on nCr.below is the pictorial representation of a pascal’s triangle.

Example:

Input : N = 5
Output:
          1
        1   1
      1   2   1
    1   3   3   1
  1   4   6   4   1
1   5   10   10   5   1

Approach #1: 

nCr formula ie- n!/(n-r)!r!

After using nCr formula, the pictorial representation becomes-



           0C0
        1C0   1C1
     2C0   2C1   2C2
  3C0   3C1   3C2   3C3

Algorithm:

  • Take a number of rows to be printed, assume it to be n
  • Make outer iteration i from 0 to n times to print the rows.
  • Make inner iteration for j from 0 to (N – 1).
  • Print single blank space ” “
  • Close inner loop (j loop) //its needed for left spacing
  • Make inner iteration for j from 0 to i.
  • Print nCr of i and j.
  • Close inner loop.
  • Print newline character (\n) after each inner iteration.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Print Pascal's Triangle in Java
import java.io.*;
  
class GFG {
    public int factorial(int i)
    {
        if (i == 0)
            return 1;
        return i * factorial(i - 1);
    }
    public static void main(String[] args)
    {
        int n = 4, i, j;
        GFG g = new GFG();
        for (i = 0; i <= n; i++) {
            for (j = 0; j <= n - i; j++) {
                System.out.print(" "); // for left spacing
            }
            for (j = 0; j <= i; j++) {
                // nCr formula
                System.out.print(
                    " "
                    + g.factorial(i)
                          / (g.factorial(i - j)
                             * g.factorial(j)));
            }
            System.out.println(); // for newline
        }
    }
}

chevron_right


Output

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

Time Complexity: O(N3)

Approach #2:

i’th entry in a line number line is Binomial Coefficient C(line, i) and all lines start with value 1. The idea is to calculate C(line, i) using C(line, i-1).

C(line, i) = C(line, i-1) * (line - i + 1) / i

Implementation:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Print Pascal's Triangle in Java
import java.io.*;
class GFG {
  
    // Pascal function
    public static void printPascal(int n)
    {
        for (int line = 1; line <= n; line++) {
  
            int C = 1; // used to represent C(line, i)
            for (int i = 1; i <= line; i++) {
                // The first value in a line is always 1
                System.out.print(C + " ");
                C = C * (line - i) / i;
            }
            System.out.println();
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 4;
        printPascal(n);
    }
}

chevron_right


Output

1 
1 1 
1 2 1 
1 3 3 1 

Time Complexity: O(N2)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :