**Pascal’s triangle **is a number pattern in a** triangular form**. It works in such a manner that 1 is present at row **0** and **1 is **running down diagonally through the left and right of the** triangle**. The numbers that are present in each step are the sum of numbers above the row. Each row in Pascal’s triangle represents a power of 2 such as the first row has 1 place i.e., 2^0, the second row has 2 is 2^1, the third row has 4 i.e., 2^2, the fourth row has 8 i.e., 2^3 and it goes on.

The write-up will discuss the working and implementation of Pascal’s triangle in Java.

**How to Print Pascal’s Triangle in Java?**

The **for loop **helps in the implementation of Pascal’s triangle**. **The code involves a traditional loop to print Pascal’s triangle for the desired number of rows.

This article will discuss the following content on how to print Pascal’s triangle in Java:

- Algorithm and pictorial representation
- Code to print Pascal’s Triangle.

**Algorithm **

For a better understanding let us look at the algorithm of **Pascal’s Triangle** first.

- Initially, only one column is present with one row that contains a value of 1.
- Now, all the rows have a value of 1 at the start and end of each column.
- The second row consists of two columns. In the successive row, the sum value of the second column is the value that has been obtained from the first and the second column.
- The sum value of the second and the third columns is positioned as a value of the third column in the successive row which is the fourth row
- Repeat the process for more rows.

A pictorial representation of Pascal’s triangle is below:

From the above representation, it will be easy for us to code **Pascal’s triangle **in Java.

**Example 1: Print Pascal’s Triangle for Number of Rows Declared**

This code below prints Pascal’s triangle for 6 rows:

```
class PascalsTriangle
{
public static void main(String[] args)
{
//Declare the number of rows, the space between values int row=6, x, y, space, n;
for(x=0; x<row; x++)
{
for(space=row; space>x; space--)
{
System.out.print(" ");
}
n=1;//Increment the value
for(y=0; y<=x; y++)
{
System.out.print(n+ " ");//Add the values accordingly.
n = n*(x-y)/(y+1);
}//Print on a separate line so that a triangle appears
System.out.print("\n");
}
}
}
```

In the code above:

- The specific number of rows has been declared.
- Space has been added to print a clean and understandable output.
- In each step, the previous column number is added along with the space.
- The sum has been printed

**Output**

In the above output, the Pascal triangle for 6 rows has been printed in a separate line.

**Example 2: User Enters the Required Number of Rows**

If the user enters the input according to the need then the above code will be modified as:

```
//Import Scanner for user inputimport java.util.Scanner;
class Pascaltriangle
{
//The main class in Java.public static void main(String[] args)
{//n= number of rows.
int n;
Scanner h = new Scanner(System.in);
System.out.print("Enter desired row size: ");
int row = h.nextInt();
for(int x=0; x<row; x++)
{
for(int space=row; space>x; space--)
{//To separate the outputs.
System.out.print(" ");
}
n=1;
for(int y=0; y<=x; y++)
{
System.out.print(n+ " ");
n = n*(x-y)/(y+1);
}
System.out.print("\n");
}
}
}
```

In the above code, the only difference from the previous code is that the scanner class has been imported to get the user input.

**Output**

The desired number of rows from the user input has been printed.

The method to print Pascal’s triangle in Java has come to an end.

**Conclusion**

In order to print Pascal’s triangle in Java there is a need to understand how it works and what parameters are required to print the triangle. Using the traditional looping method Pascal’s triangle can be printed by entering the required number of rows. The blog has demonstrated the implementation and the way Pascal’s triangle works in Java.