Pascals Triangle

Java 13.2.2011 12 Comments


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[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

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

12 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.

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

    please help..

  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

    Please solve all pattern …

    6-

    Hi please solve this pattern..

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

  11. Akarshit

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

Leave a Reply

s2Member®