I recently started learning Java for a class and my teacher gave us an assignment in which we had to write up code which would print out the Pascal's triangle to an amount of rows decided by the user, and use recursion to do so. I believe that I'm done with the assignment, but wanted to know, for myself, as to how I could improve and condense this messy code (the array complicates things but I wanted to use it for a different program that expands polynomials (\$(x+b)^n)\$).

```
import java.io.*;
import java.util.*;

public class PascalTriangleYar
{
private static int[][] xArr;
private static int g =2;

public void printArray(int[][] xArray)
{

  for(int r=0; r<xArray.length; r++){
  System.out.println("");
     for(int c=0; c<xArray[r].length; c++)
        if(xArray[r][c]==0)
           System.out.print("\t");
        else
           System.out.print(xArray[r][c]+"\t");}

}

public int[][] getTri()
{
return xArr;}

public void pTriangle(int r)
{
  int n=r;
  xArr = new int[r][2*r-1];
  xArr[0][r-1]=(1);
  p2T(r, 1);
  printArray(xArr);
}

private void p2T(int r, int n)
{
  int z=0;

  int p=r-g;

    xArr[n][p]=(1);

  for(int fd=0; fd<xArr[0].length-1; fd++){
  if(fd==0)
     xArr[n][fd]= xArr[n-1][0]+xArr[n-1][1];
  else
     xArr[n][fd]= xArr[n-1][fd-1] + xArr[n-1][fd+1];

     xArr[r-1][2*r-2]=1;   

  }

  g++;
  if(n<r-1)
  {
     p2T(r,n+1);
  }
 // else
 // {return;}

}  

public static void main(String [] args)
{
  PascalTriangleYar tri = new PascalTriangleYar();
  Scanner inputReader = new Scanner(System.in);
  System.out.println("Enter a number (x). A pascal's triangle of x rows will be printed.");
  int x = inputReader.nextInt();
  tri.pTriangle(x);
}
}

```