pascal's triangle java 2d array

import java.util.Scanner;
class Pascal_Triangle
{//opening of class
    public static void main(String args[])
    {//opening of main
        Scanner sc=new Scanner(System.in);
        int n,i,j,a[][];
        //taking user's input.
        System.out.println("HOW MANY STEPS?");
        n=sc.nextInt();
        a=new int[n][n];
        //filling the 2D matrix.
        for(i=0;i<n;i++){
            for(j=0;j<=i;j++)
                if(j==0 || j==i)
                    a[i][j]=1;
                else
                    a[i][j]=a[i-1][j-1]+a[i-1][j];
        }
        //displaying the Pascal's Triangle as the output.
        System.out.println("\nOUTPUT:\n");
        for(i=0;i<n;i++)
        {
            for(j=0;j<=i;j++)
                System.out.print(a[i][j]+"\t");

            System.out.println();
        }
    }//clossing of main
}//closing of class

4
6
Leslie Webb 145 points

                                    import java.util.Scanner;
class Pascal_Triangle
{//opening of class
    public static void main(String args[])
    {//opening of main
        Scanner sc=new Scanner(System.in);
        int n,i,j,a[][],s;
        //taking user's input.
        System.out.println(&quot;HOW MANY STEPS?&quot;);
        n=sc.nextInt();
        s=n-1; 
        a=new int[n][n];
        //filling the 2D matrix.
        for(i=0;i&lt;n;i++){
            for(j=0;j&lt;=i;j++)
                if(j==0 || j==i)
                    a[i][j]=1;
                else
                    a[i][j]=a[i-1][j-1]+a[i-1][j];
        }
        //displaying the Pascal's Triangle as the output.
        System.out.println(&quot;\nOUTPUT:\n&quot;);
        for(i=0;i&lt;n;i++)
        {
            for(j=0;j&lt;=s;j++)
            System.out.print(&quot; &quot;);//printing blank spaces at the beginning of rows
            
            s--;
            
            for(j=0;j&lt;=i;j++)
                System.out.print(a[i][j]+&quot; &quot;);

            System.out.println();
        }
    }//clossing of main
}//closing of class

4 (7 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source