Test Pascal Triangle in Java

Pascal Triangle.
    1
   11
  121
 1331
14641

its just interview question, to how to create pascal triangle.
It is just Pascal Triangle Program , That how its work.


And what is logic to how to create a pascal program.

ex:
class demo{
public static void main(String args[]){
int row = 5;
for(int i = 0;i<row;i++){
      for(int k =row;k>i;k--){
            System.out.print(" ");
      }
      int num = 1;
      for(int j=0;j<=i;j++){
             System.out.println(num+" ");
             num = num*(i-j)/(j+1);
      }
       System.out.println();
  }
}
}

logic...


First it count , How many row your are create.
Then decide, how many space require to create pascal triangle..
So that, K for loop is use.
Like,
_ _ _ _ 1
_ _ _1
_ _ 1
_ 1
1
Then,
the integer num variable is print every first time print 1 as per above.
then calculate the num.
and next line.
its calculate 1, 2 , 3, 4 ,6 and one by one print
so that output generate

     1
   11
  121
 1331
14641

*****************************************************************


Comments