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

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[0]; for (int i=0; i<n; i++) { // Calculate next row row = pascalRow(row); // Output row // First add some padding so triangle is centered 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(); } } // Full source available for Download // See below for details }

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

x

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.

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

Thank You.

Learn Java by ExampleYou would need to rename the main() method, and then call that method from your driver class.

Sandy Leethank 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.

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

Sandy Leeso this line would be come like so?

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

Using a List instead of an array | Learn Java by Example[…] we showed you how to generate Pascals Triangle and in that Java example we used arrays to represent each row of the […]

Learn Java By ExampleHave posted a version showing the use of List instead of array.

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

navneetHi all i want to print

*

..

***

….

*****

In pyramid form

please help..

mini rastogiHow to solve this pattern

@@@@@

? ? ? ?

@@@

? ?

@

mini rastogi1-

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

Please solve all pattern …

6-

Hi please solve this pattern..

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

sharmila1

112

1122

11223

112233

i want this output coding using array concepts

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

sajrinhi i need a simple program on pascal’s triangle