How To Write Pascals Triange Program in Java

In mathematics, Pascal’s triangle is a triangular array of the binomial coefficients. It is named after a famous French Mathematician and Philosopher Blaise Pascal. To build the triangle, start with “1” at the top, then continue placing numbers below it in a triangular pattern.


 Each number is the two numbers above it added together (except for the edges, which are all “1”). (In the image above, 1 + 3 = 4) is highlighted. In Java, 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. Below is an animation of the pascal triangle that shows how Pascal’s Triangle generates.

Copy the source code below and paste it in your favorite Java IDE. I am using JCreator because that is what my lecturer used to teach me Java. However, whichever one you choose, the program should work fine. You can check about the best Java IDE students can use for application development to get familiar with Java IDEs.


There should be no errors. If you have an error, please check and see if the class name is Pascal. If you give it a different class name, the program is not going to work. Also make sure that your class name do not start with a small letter, in Java the name of a class must start with a capital letter. Good luck.

 public class Pascal{
//Source Code of the Pascals Triangle in Java (5 rows)
     public static void main(String args[]) {
         
        int n = 5;           // Number of rows
         
        int[] row = new int[0];
         
        for(int i=0 ; i < n ; i++){
             
            row = nextRow(row);
             
            for(int j=0;j < n-i;j++){
                 
                //Padding For Triangle
                System.out.print(" ");
                 
            }
            //Output the values
            for(int j=0 ; j < row.length ; j++){
                 
                System.out.print(row[j]+" ");
         
            }
            //Start New Line
            System.out.println();
        }
    }
    /*Find Values Of Next Row*/
    public static int[] nextRow(int row[]){
         
        int nextRow[] = new int [row.length+1];
         
        nextRow[0] = 1;               //First element always be 1
        nextRow[nextRow.length-1] =1; //Last element should be 1
         
        //generating rest values from the previous row
        for(int i=1 ; i < nextRow.length-1 ; i++){
             
            nextRow[i] = row[i-1] + row[i];
        }
        return nextRow;
    }
     
}
 
 

The output of the program is shown below:

Hi! My name is Ikenna Gabriel Ifenna and I am a professional IT consultant. I have an excellent understanding of how computer works and I love to read, write and research about tech kinds of stuff. I design websites, knows how to use WordPress multisite to create complex web applications and I design e-Commerce websites for businesses. When I am not doing any of these, you can find me riding bicycle with Strava. Want to work together? I would love to hear from you.

What do you want to do? Tell us in the comments.
Register New Account
Reset Password