Java – Pascal’s Triangle Patterns

Pattern:

        1
      1 2 1
    1 2 3 2 1
  1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

Code:

int r=6, c=1;
for(int i=0; i<r ; i++) {
            for(int s=1; s<r-i; s++) {
                        System.out.print(”  “);
            }
            for(int j=0; j<=i; j++) {
                        if (j==0 || i==0)
                                    c=1;
                        else
                                    c=c*(i-j + 1)/j;
                        System.out.printf(“%4d”, c);
            }
            System.out.println();
}

Pattern:

123454321
 1234321
  12321
   121
    1
for (int i = 5; i >= 1; i–){
            for (int j = 5 – i; j >= 1; j–){
                        System.out.print(” “);
            }
            for (int j = 1; j <= i; j++){
                        System.out.print(j);
            }
            for (int j = i – 1; j >= 1; j–){
                        System.out.print(j);
            }
            System.out.println();
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top