## Pascals Triangle Write a Java application that prints the first 10 lines of Pascals Triangle.

Each row of a Pascals Triangle can be calculated from the previous row so the core of the solution is a method that calculates a row based on the previous row which is passed as input. Once we have that it is simply a matter of calling that method in a loop and formatting each row of the triangle.

You will notice that some padding is added to center the triangle output. This uses the row number to determine the amount of padding to use. This is not a perfect solution because it assumes that the numbers in the triangle are only single digits (which is not the case after row 5) but its a sufficient approximation for this example.
x
```package com.learnjava.math;

/**
* Write a Java program that calculates the
* first 10 lines of a Pascals Triangle
*
* @author http://learn-java-by-example.com
*
*/

public class PascalTriangle {

public static void main(String[] args) {

int n = 10;

int[] row = new int;

for (int i=0; i<n; i++) {

// Calculate next row

row = pascalRow(row);

// Output row

for (int j=0; j< n - i; j++) {
System.out.print(" ");
}

// Now output the row values

for (int j=0; j< row.length; j++) {
System.out.print(row[j]+" ");
}
System.out.println();

}
}

// See below for details
}

```
Full source code for this example including the pascalRow() method is available for download for our members.
x

If you found this problem a little challenging then we would suggest having a look at some simpler examples of printing triangles using Java.

### 14 Responses to “Pascals Triangle”

1. Sandy Lee

Hello i was wondering how to divide this pascalsTriangle class into two classes, where one is the driver class.

Thank You.

• Learn Java by Example

You would need to rename the main() method, and then call that method from your driver class.

2. Sandy Lee

thank you! i knew it but i just forgot to do PascalTriangle pas = new PascalsTriangle();

however i have another question.

How would i do this without using arrays i was told to try it using one arraylist.

Thank You.

3. Learn Java By Example

You can replace the use of the array with a List. So for example row[i] would become row.get(i)

4. Sandy Lee

so this line would be come like so?

row.get(i) = previous.get(i-1) + previous.get(i);

5. […] we showed you how to generate Pascals Triangle and in that Java example we used arrays to represent each row of the […]

6. Learn Java By Example

Have posted a version showing the use of List instead of array.

http://learn-java-by-example.com/2012/java/list-array

7. navneet

Hi all i want to print

*
..
***
….
*****
In pyramid form

8. mini rastogi

How to solve this pattern
@@@@@
? ? ? ?
@@@
? ?
@

9. mini rastogi

1-

1
01
101
0101
10101
&
Reverse above pattern

2-

12345
2345
345
45
5
& also
Reverse pattern

3-

54321
4321
321
21
1

4-

*
##
***
####
*****

5-
10101
0101
101
01
1

6-

10. Learn Java By Example

We have just added 5 new examples covering different variations of printing triangles and pyramids using Java.

11. sharmila

1
112
1122
11223
112233

i want this output coding using array concepts

12. Akarshit

Hi i want a simple pascals triangle in array with scanner package

13. sajrin

hi i need a simple program on pascal’s triangle