3

I need to write a Java-Code which prints the pascal's triangle. This is what I did until now.

import java.util.Scanner;

class Pascal {

    static int bnk (int n, int k) {

        if (k==0 || k==n) {
            return 1;
        } // B(n,k) Berechnung für Standardwert = 1;
        else {
            int x = (bnk(n-1, k-1) + bnk(n-1, k));
            return x;
        } // Berechnung aller sonstigen B(n,k)-Werte. 
    } // Berechnung von B(n,k)

    public static void main (String [] args) {

        Scanner sc = new Scanner(System.in);

        System.out.println("How many rows?: ");
        int r = sc.nextInt();

        sc.close();

        for (int n=0; n<r; n++) {

            for (int j=0; j<(r-n); j++) {
                System.out.print(" "); 
            } // Setzt Anzahl Leerzeichen vor erster Zahl

            for (int k=0; k<=n; k++) {

                int b = bnk(n,k);
                System.out.print(b+" ");
            } // Berechnet in jeder Reihe alle Elemente

            System.out.println();
        } // Berechnet Reihe nach Reihe
    } // main
} // class Pascal

The first method calculates the values needed in the triangle. The second one prints the triangle row after row by printing (r-n) blanks. n stands for the current row and r is the total count of rows. So the left side of the triangle gets printed in the right way but the problem is, when the values in the triangle are getting too high, the triangle gets out of shape on the right side. I hope what i described was understandable. Could you help me find a way to properly format the triangle?

The current output looks like this:

5 rows: https://gyazo.com/d9a536d3ac92c155707ebb2e4ee7745b

enter image description here

10 rows: https://gyazo.com/4ab0479f9324dd7c2911398ea5a71e33

enter image description here

Frakcool
  • 10,915
  • 9
  • 50
  • 89
Tim Buchholz
  • 83
  • 1
  • 3
  • 10
  • Maybe you could add to your question your current output (to illustrate) –  Dec 16 '16 at 16:43
  • I tried adding output, but it wouldn't let me edit it correctly for the OP. Anyways, the problem you are having is that you aren't making space to multiple digit numbers in your triangle. You should put more spaces between the numbers and I suspect it'll start looking better. – DejaVuSansMono Dec 16 '16 at 16:52

2 Answers2

4

So you could format with "\t" but instead of using 1, use 2. Then you would be able to space the numbers every 1 space or every 2 spaces, allowing the first (top) one to be in between the second 2, and repeat.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
ben
  • 41
  • 2
  • Could you perhaps briefly write a piece of code to show you mean? I am still pretty new to java and i don't really get what you are meaing to do. – Tim Buchholz Dec 16 '16 at 17:03
  • @TimBuchholz he means that instead of printing `System.out.print(" ");` use `System.out.print("\t");` or `System.out.print("\t\t");` Where `"\t"` is an escape character for a tabulation – Frakcool Dec 16 '16 at 17:08
  • I tried that and it doesn't work like you'd think it would. It makes it a right angle triangle instead of the Pascal he is looking for. Cool, but it doesn't solve the issue. – DejaVuSansMono Dec 16 '16 at 17:11
2

Simple solution:

Compute the maximum of the digits that are to be displayed, in your case 3 for 126. Then, every number is displayed using this amount of space, with leading or trailing spaces to fill it up.

More complex solution, that might not be better, depending on your taste:

Given n lines of numbers to be displayed

  1. Initialize a list positions_n, which stores the positions at which each number in the last line is to be displayed. In your example, the last line is

1 9 36 84 126 126 84 36 9 1

Thus, positions_n would be set to 0, 2, 4, 7, 10, 14, 18, 21, 24, 26 (if I did no mistake)

  1. Iterating backwards, generate positions_i-1 from positions_i by taking the mean of two positions. For the example of positions_n, positions_n-1 would thus be (assuming we round up):

1, 3, 6, 12, 16, 20, 23, 25

  1. Display the numbers at those positions.

Problem with this is of course that we will have jumps that might look bad whenever we need to round. Also, the spacing between the numbers could get weird and uneven.

Aziuth
  • 3,652
  • 3
  • 18
  • 36