1

I'm trying to create a recursive method that prints the Pascal's triangle both upside-down and rightside-up. I want to use a boolean variable to switch between an upside down triangle and a rightside-up triangle. So far, I have successfully written the code to make the pascal's triangle upside down:

public class Main {
    boolean upsideDown = true;

    public void printPascal(int n) {
        if (!upsideDown) {
            //THIS IS WHERE I'M STUCK
        }
        if (upsideDown) {
            if (n < 0)
                return;
            for (int k = 0; k <= n; k++) {
                System.out.print(binom(n, k) + " ");
            }
            System.out.println();
            printPascal(n - 1);
        }
    }

    public int binom(int n, int k) {
        if (k == n || k == 0)
            return 1;
        else return binom(n - 1, k - 1) + binom(n - 1, k);
    }

    public static void main(String[] args) {
        new Main().printPascal(10); //This will print the Pascal's triangle
        //from levels 10 to 0
    }
}

So now, I'm trying to print the Pascal's triangle the rightside-up (from levels 0 to 10). How can I make a recursive method that achieves this?

I know there are lots of code online about Pascal's triangle. But I can't find anything that deals with my issue in particular.

TheSaviour
  • 61
  • 1
  • 7

3 Answers3

2

It seems like the most elegant solution is to change the print order, so that the last step is printed first, followed by the second to last etc, with the first step printed last. Would this work?

public void printPascal(int n) {
    if (!upsideDown) {
        if (n < 0)
            return;
        //print the future step first, then print current step
        printPascal(n - 1);
        System.out.println();
        for (int k = 0; k <= n; k++) {
            System.out.print(binom(n, k) + " ");
        }
    }
    if (upsideDown) {
        if (n < 0)
            return;
        for (int k = 0; k <= n; k++) {
            System.out.print(binom(n, k) + " ");
        }
        System.out.println();
        printPascal(n - 1);
    }
}
Assafs
  • 3,257
  • 4
  • 26
  • 39
1

This is a summary of the first two answers, and here we can input the number of necessary lines that we need in pascal's triangle.

public class Main {
    boolean upsideDown = true;
    public void printPascal(int n) {
        if (!upsideDown) {
            if (n < 0)
                return;
            printPascal(n - 1);
            System.out.println();
            for (int k = 0; k <= n; k++) {
                System.out.print(binom(n, k) + " ");
            }
        }
        if (upsideDown) {
            if (n < 0)
                return;
            for (int k = 0; k <= n; k++) {
                System.out.print(binom(n, k) + " ");
            }
            System.out.println();
            printPascal(n - 1);
        }
    }
    public int binom(int n, int k) {
        if (k == n || k == 0)
            return 1;
        else return binom(n - 1, k - 1) + binom(n - 1, k);
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int number = input.nextInt();
        new Main().printPascal(number - 1);
    }
}
1

You can iteratively create a 2d array with Pascal's triangle and then print it in the upper left corner as follows:

 1  1  1  1  1  1  1  1  1  1 
 1  2  3  4  5  6  7  8  9 
 1  3  6 10 15 21 28 36 
 1  4 10 20 35 56 84 
 1  5 15 35 70 126 
 1  6 21 56 126 
 1  7 28 84 
 1  8 36 
 1  9 
 1 

Code:

int n = 10;
// an array of 'n' rows
int[][] triangle = new int[n][];
// iterate over the rows of the array
for (int i = 0; i < n; i++) {
    // a row of 'n-i' elements
    triangle[i] = new int[n - i];
    // iterate over the elements of the row
    for (int j = 0; j < n - i; j++)
        if (i == 0 || j == 0)
            // elements of the first row
            // and column are equal to one
            triangle[i][j] = 1;
        else
            // all other elements are the sum of the
            // previous element in the row and column
            triangle[i][j] = triangle[i][j - 1] + triangle[i - 1][j];
}
// output
for (int[] row : triangle) {
    for (int element : row)
        // format as a two-digit number
        System.out.printf("%2d ", element);
    System.out.println();
}

See also: Pascal's triangle 2d array - formatting printed output