pascal 27s triangle java 2d array

Solutions on MaxInterview for pascal 27s triangle java 2d array by the best coders in the world

showing results for - "pascal 27s triangle java 2d array"
Alessio
15 Mar 2018
1import java.util.Scanner;
2class Pascal_Triangle
3{//opening of class
4    public static void main(String args[])
5    {//opening of main
6        Scanner sc=new Scanner(System.in);
7        int n,i,j,a[][],s;
8        //taking user's input.
9        System.out.println("HOW MANY STEPS?");
10        n=sc.nextInt();
11        s=n-1; 
12        a=new int[n][n];
13        //filling the 2D matrix.
14        for(i=0;i<n;i++){
15            for(j=0;j<=i;j++)
16                if(j==0 || j==i)
17                    a[i][j]=1;
18                else
19                    a[i][j]=a[i-1][j-1]+a[i-1][j];
20        }
21        //displaying the Pascal's Triangle as the output.
22        System.out.println("\nOUTPUT:\n");
23        for(i=0;i<n;i++)
24        {
25            for(j=0;j<=s;j++)
26            System.out.print(" ");//printing blank spaces at the beginning of rows
27            
28            s--;
29            
30            for(j=0;j<=i;j++)
31                System.out.print(a[i][j]+" ");
32
33            System.out.println();
34        }
35    }//clossing of main
36}//closing of class
Luis
19 Aug 2020
1import java.util.Scanner;
2class Pascal_Triangle
3{//opening of class
4    public static void main(String args[])
5    {//opening of main
6        Scanner sc=new Scanner(System.in);
7        int n,i,j,a[][];
8        //taking user's input.
9        System.out.println("HOW MANY STEPS?");
10        n=sc.nextInt();
11        a=new int[n][n];
12        //filling the 2D matrix.
13        for(i=0;i<n;i++){
14            for(j=0;j<=i;j++)
15                if(j==0 || j==i)
16                    a[i][j]=1;
17                else
18                    a[i][j]=a[i-1][j-1]+a[i-1][j];
19        }
20        //displaying the Pascal's Triangle as the output.
21        System.out.println("\nOUTPUT:\n");
22        for(i=0;i<n;i++)
23        {
24            for(j=0;j<=i;j++)
25                System.out.print(a[i][j]+"\t");
26
27            System.out.println();
28        }
29    }//clossing of main
30}//closing of class
similar questions
queries leading to this page
pascal 27s triangle java 2d array