Challenge 4: Pascal's Triangle

In this challenge, you have to implement Pascal's triangle using a two-dimensional Array.

Problem statement

Implement a Java method that takes an integer size as input and displays a table that represents a Pascal’s triangle using a two-dimensional array.

Function prototype

void printPascalTri(int size)

Sample input

int size = 5;

Sample output

Print Pascal’s triangle of the size 5.
Pascal’s triangle is filled from the top towards the bottom. In Pascal’s triangle:

  • first and the second rows are set to 1.
  • Each element of the triangle (from the third row downward) is the sum of the element directly above it and the element to the left of the element directly above it.

Note: In order to move a value to the next line you can use \n.

Pictorial representation

See the example Pascal triangle(size=5) below:

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

Get hands-on with 1200+ tech skills courses.