12

The assignment is to create Pascal's Triangle without using arrays. I have the method that produces the values for the triangle below. The method accepts an integer for the maximum number of rows the user wants printed.

public static void triangle(int maxRows) {
    int r, num;
    for (int i = 0; i <= maxRows; i++) {
        num = 1;
        r = i + 1;
        for (int col = 0; col <= i; col++) {
            if (col > 0) {
                num = num * (r - col) / col;    
            }
            System.out.print(num + " ");
        }
        System.out.println();
    }
}

I need to format the values of the triangle such that it looks like a triangle:

              1
            1   1
          1   2   1
        1   3   3   1
      1   4   6   4   1
    1   5  10  10   5   1
  1   6  15  20  15   6   1

I can't for the life of me figure out how to do that. Please answer keeping in mind that I'm a beginner in Java programming.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Sidarth Shahri
  • 121
  • 1
  • 1
  • 3
  • 1
    You might also need some extra spaces between your numbers, because you want the single digit numbers to sit further apart than the double digit numbers. – Dawood ibn Kareem Nov 12 '13 at 01:16

8 Answers8

12
public static long pascalTriangle(int r, int k) {
    if (r == 1 || k <= 1 || k >= r) return 1L;
    return pascalTriangle(r - 1, k - 1) + pascalTriangle(r - 1, k);
}

This method allows you to find the k-th value of r-th row.

Community
  • 1
  • 1
Boa Hancock
  • 121
  • 1
  • 3
8

This is a good start, where it's homework, I'll leave the rest to you:

int maxRows = 6;
int r, num;
for (int i = 0; i <= maxRows; i++) {
    num = 1;
    r = i + 1;
    //pre-spacing
    for (int j = maxRows - i; j > 0; j--) {
        System.out.print(" ");
    }
    for (int col = 0; col <= i; col++) {
        if (col > 0) {
            num = num * (r - col) / col;
        }
        System.out.print(num + " ");
    }
    System.out.println();
}

Output:

      1 
     1 1 
    1 2 1 
   1 3 3 1 
  1 4 6 4 1 
 1 5 10 10 5 1 
1 6 15 20 15 6 1 
Community
  • 1
  • 1
Uncle Iroh
  • 5,748
  • 6
  • 48
  • 61
1

In each row you will need to print:

  • n spaces
  • m numbers
  • n spaces

Your job is to figure out n (which will be zero in the last line) and m based on row number.

[This is more like a comment but I needed more formatting options than comments provide]

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
1

You need to print the spaces (like others have mentioned) and also as this is homework I'm leaving it to you but you might want to look at this handy little function

System.out.printf();

Here is a handy reference guide

Also note that you will need to take into account that some numbers are more than 1 digit long!

Java Devil
  • 10,629
  • 7
  • 33
  • 48
1
import java.util.*;
class Mine {
    public static void main(String ar[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 1; i < n; i++) {
            int size = 1;
            for (int j = 1; j <= i; j++) {
                int a[] = new int[size];
                int d[] = new int[size];
                for (int k = 1; k <= size; k++) {
                    a[1] = 1;
                    a[size] = 1;

                    for (int p = 1; p <= size; p++) {
                        d[p] = a[p];
                    }

                    if (size >= 3) {
                        for (int m = 2; m < size; m++) {
                            a[m] = d[m] + d[m - 1];
                        }
                    }
                }

                for (int y = 0; y < size; y++) {
                    System.out.print(a[y]);
                }
                System.out.println(" ");
            }
            ++size;
        }
    }
}
0
public class HelloWorld {
    public static void main(String[] args) {
        int s = 7;
        int k = 1;
        int r;

        for (int i = 1; i <= s; i++) {
            int num = 1;
            r = i;
            int col = 0;
            for (int j = 1; j <= 2 * s - 1; j++) {
                if (j <= s - i)
                    System.out.print("  ");
                else if (j >= s + i)
                    System.out.print("  ");
                else {
                    if (k % 2 == 0) {
                        System.out.print("  ");
                    } else {
                        if (col > 0) {
                            num = num * (r - col) / col;
                        }
                        System.out.print(num + " ");
                        col++;
                    }
                    k++;
                }
            }
            System.out.println("");
            k = 1;
        }
    }
}

Output:

            1             
          1   1           
        1   2   1         
      1   3   3   1       
    1   4   6   4   1     
  1   5   10   10   5   1   
1   6   15   20   15   6   1 
Community
  • 1
  • 1
Irf92
  • 319
  • 2
  • 14
0

You can try this code in java. It's simple :)

public class PascalTriangle {
    public static void main(String[] args) {
        int rows = 10;
        for (int i = 0; i < rows; i++) {
            int number = 1;
            System.out.format("%" + (rows - i) * 2 + "s", "");
            for (int j = 0; j <= i; j++) {
                System.out.format("%4d", number);
                number = number * (i - j) / (j + 1);
            }
            System.out.println();
        }
    }
}

Output:

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

Code perfectly prints pascal triangle:

public static void main(String[] args) {
    int a, num;

    for (int i = 0; i <= 4; i++) {
        num = 1;
        a = i + 1;

        for (int j = 4; j > 0; j--) {
            if (j > i)
                System.out.print(" ");
        }

        for (int j = 0; j <= i; j++) {
            if (j > 0)
                num = num * (a - j) / j;

            System.out.print(num + " ");
        }
        System.out.println();
    }
}

Output:

    1 
   1 1 
  1 2 1 
 1 3 3 1 
1 4 6 4 1 
Community
  • 1
  • 1