Tuesday 16 August 2016
0 comments

Pascal triangle in Java


 Print the Following Pascal triangle using     
 irregular multidimensional array.

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1



Source Code:
*************************************************************************

public class Pascal{

public static void main(String [] args) {

        int [][] a = new int[5][];

        for (int i=0; i<a.length; i++) {
                a[i] = new int[i+1];
                a[i][0] = 1;
                a[i][i] = 1;
                for (int j=1; j<i; j++) {
                        a[i][j] = a[i-1][j]+a[i-1][j-1];
                        }
                }

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

}



*******************************************************************************************
Output:-

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1


0 comments:

Post a Comment

 
Toggle Footer
Top