Pascal Triangle In Java ISC

-Program to generate a Pascal’s Triangle

class PascalTriangle {
//start of class
public void accept(int n) {
int a[][]=new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
if(i==j||j==0)
a[i][j]=1;
else
a[i][j]=a[i-1][j]+a[i-1][j-1];
}}
for(int i=0;i<n;i++){
for(int k=1;k<n-1;k++){
System.out.println(" ");
}
for(int j=0; j<=i;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}}}//end of class

OUTPUT---
1



1 1



1 2 1



1 3 3 1



1 4 6 4 1 

Comments

Popular posts from this blog

Sort Boundary Elements Of A Matrix

Lucky Number In Java ISC

Program to fill a square matrix of size ‘n*n” in a circular fashion (clockwise) with natural numbers from 1 to n*n, taking ‘n’ as input.