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
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
Post a Comment