Skip to main content

Pattern Programs in Java: 30 Star, Number and Character Patterns (with Logic Explained)

30 pattern programs in Java with real output, the row/column logic table and the code for each: pyramids, diamonds, Floyd’s and Pascal’s triangle, hollow shapes and a method for any pattern.

A pattern program prints a shape of stars, numbers or letters using nested loops. The outer loop walks the rows; the inner loops decide, for each row, how many spaces and how many symbols to print. If you can fill in a table of row → spaces, symbols, the code writes itself.

This page has 30 patterns that cover the whole ICSE, school and college syllabus: triangles, pyramids, diamonds, hollow shapes, Floyd’s triangle, Pascal’s triangle, alphabet patterns and a spiral matrix. Every pattern shows its real output, the row/column logic table and the code. Every table row was checked against the program’s output, not typed by hand.

Tested on OpenJDK 21.0.12. The programs use nothing newer than Java 8 syntax, so they compile on any JDK from 8 onwards (I ran them on 21 only). The whole file, PatternPrograms.java, is here: download PatternPrograms.java. Compile with javac PatternPrograms.java, run all 30 with java PatternPrograms or one pattern with java PatternPrograms 04 5.

The thinking method that works for any pattern

Do not start by typing loops. Start with the row table. For the pyramid below, row 1 has four spaces and one star, row 2 has three spaces and two stars, and so on. The blue cells are stars and the dashed cells are spaces.

*row 1: 4 spaces, 1 star**row 2: 3 spaces, 2 stars***row 3: 2 spaces, 3 stars****row 4: 1 spaces, 4 stars*****row 5: 0 spaces, 5 stars

The picture is the logic table of pattern 4. Spaces fall by one per row (n − i) while stars rise by one (i). Each of those two numbers becomes one inner for loop, and the outer loop supplies i.

  1. Count the rows. That is the outer loop, usually 1..n, or 1..2n−1 for a diamond.
  2. For one row, write down the leading spaces as a formula in i and n.
  3. Write down how many symbols the row prints and what each one is: a star, j, i or a letter.
  4. End the row with System.out.println(), after the inner loops, not inside them.

For shapes with an empty middle, replace step 3 with a yes/no question per cell: is this cell on the border? The hollow patterns are exactly that.

Every symbol is two characters wide in the “* ” patterns. If you print "* " (star plus space) you must print two spaces for each skipped column, as in pattern 3. In the pyramid we shift by single spaces on purpose: half a star per row is what makes the slant.

All 30 patterns at a glance

#Pattern
1Right triangle of stars
2Inverted right triangle
3Right-aligned triangle
4Pyramid
5Inverted pyramid
6Diamond
7Hollow square
8Hollow right triangle
9Hollow pyramid
10Hollow diamond
11Solid square
12Number triangle (1, 12, 123)
13Same number on each row
14Inverted number triangle
15Number pyramid (1, 121, 12321)
#Pattern
16Floyd’s triangle
17Pascal’s triangle
18Alphabet triangle (A, AB, ABC)
19Same letter on each row
20Alphabet pyramid (A, ABA, ABCBA)
21Binary triangle (1, 01, 101)
22Butterfly
23Hourglass
24Right arrow (half diamond)
25X pattern
26Palindromic number pyramid
27Spiral matrix
28Checkerboard
29Plus sign
30Number diamond

Star triangles and pyramids (patterns 1–6)

The rows grow, shrink or shift. Master these six and the other 24 are variations.

Pattern 1: Right triangle of stars

Row i prints i stars.

*
* *
* * *
* * * *
* * * * *
Row iLeading spaces = 0Items printed = i
101
202
303
404
505
static void p01(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) System.out.print("* ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p01. The output above is copied from running it with n = 5.

Pattern 2: Inverted right triangle

Row i prints n βˆ’ i + 1 stars, so the first row is full and the last has one.

* * * * *
* * * *
* * *
* *
*
Row iLeading spaces = 0Items printed = n βˆ’ i + 1
105
204
303
402
501
static void p02(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i + 1; j++) System.out.print("* ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p02. The output above is copied from running it with n = 5.

Pattern 3: Right-aligned triangle

Each star is printed as two characters ("* "), so every skipped column costs two spaces.

        *
      * *
    * * *
  * * * *
* * * * *
Row iLeading spaces = 2(n βˆ’ i)Items printed = i
181
262
343
424
505
static void p03(int n) {
    for (int i = 1; i <= n; i++) {
        for (int s = 1; s <= n - i; s++) System.out.print("  ");
        for (int j = 1; j <= i; j++) System.out.print("* ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p03. The output above is copied from running it with n = 5.

Pattern 4: Pyramid

The leading spaces shift each row half a star to the right, and "* " printed i times makes the row grow by one star.

    *
   * *
  * * *
 * * * *
* * * * *
Row iLeading spaces = n βˆ’ iItems printed = i
141
232
323
414
505
static void p04(int n) {
    for (int i = 1; i <= n; i++) {
        for (int s = 1; s <= n - i; s++) System.out.print(" ");
        for (int j = 1; j <= i; j++) System.out.print("* ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p04. The output above is copied from running it with n = 5.

Pattern 5: Inverted pyramid

The pyramid upside down: spaces grow by one per row while stars shrink by one.

* * * * *
 * * * *
  * * *
   * *
    *
Row iLeading spaces = i βˆ’ 1Items printed = n βˆ’ i + 1
105
214
323
432
541
static void p05(int n) {
    for (int i = 1; i <= n; i++) {
        for (int s = 1; s <= i - 1; s++) System.out.print(" ");
        for (int j = 1; j <= n - i + 1; j++) System.out.print("* ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p05. The output above is copied from running it with n = 5.

Pattern 6: Diamond

Rows 1..2nβˆ’1. Let k = i ≀ n ? i : 2n βˆ’ i: it climbs to n and falls back, so one loop draws both halves.

    *
   * *
  * * *
 * * * *
* * * * *
 * * * *
  * * *
   * *
    *
Row iLeading spaces = n βˆ’ kItems printed = k
141
232
323
414
505
614
723
832
941
static void p06(int n) {
    for (int i = 1; i <= 2 * n - 1; i++) {
        int k = i <= n ? i : 2 * n - i;          // stars on this row
        for (int s = 1; s <= n - k; s++) System.out.print(" ");
        for (int j = 1; j <= k; j++) System.out.print("* ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p06. The output above is copied from running it with n = 5.

Hollow and solid shapes (patterns 7–11)

Same loops, but each cell asks a yes/no question: am I on the border?

Pattern 7: Hollow square

A cell is a star only if it is on the border: i == 1 || i == n || j == 1 || j == n. Everything else prints two spaces to keep the grid aligned.

* * * * *
*       *
*       *
*       *
* * * * *
Row iLeading spaces = 0Items printed = n on rows 1 and n, otherwise 2
105
202
302
402
505
static void p07(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            boolean border = i == 1 || i == n || j == 1 || j == n;
            System.out.print(border ? "* " : "  ");
        }
        System.out.println();
    }
}

Source: PatternPrograms.java, method p07. The output above is copied from running it with n = 5.

Pattern 8: Hollow right triangle

Stars on the first column, on the diagonal (j == i) and on the whole last row.

*
* *
*   *
*     *
* * * * *
Row iLeading spaces = 0Items printed = 1, then 2, then n on the last row
101
202
302
402
505
static void p08(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) {
            boolean edge = j == 1 || j == i || i == n;
            System.out.print(edge ? "* " : "  ");
        }
        System.out.println();
    }
}

Source: PatternPrograms.java, method p08. The output above is copied from running it with n = 5.

Pattern 9: Hollow pyramid

Row i is 2i βˆ’ 1 columns wide. Only its first and last column hold a star, except the base row.

    *
   * *
  *   *
 *     *
*********
Row iLeading spaces = n βˆ’ iCharacters printed = 1, then 2, then 2n βˆ’ 1 on the last row
141
232
322
412
509
static void p09(int n) {
    for (int i = 1; i <= n; i++) {
        for (int s = 1; s <= n - i; s++) System.out.print(" ");
        for (int j = 1; j <= 2 * i - 1; j++) {
            boolean edge = j == 1 || j == 2 * i - 1 || i == n;
            System.out.print(edge ? "*" : " ");
        }
        System.out.println();
    }
}

Source: PatternPrograms.java, method p09. The output above is copied from running it with n = 5.

Pattern 10: Hollow diamond

The diamond loop with the hollow rule from the hollow pyramid: print a star only at j == 1 and j == 2k βˆ’ 1.

    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *
Row iLeading spaces = n βˆ’ kCharacters printed = 1 at the tips, otherwise 2
141
232
322
412
502
612
722
832
941
static void p10(int n) {
    for (int i = 1; i <= 2 * n - 1; i++) {
        int k = i <= n ? i : 2 * n - i;
        for (int s = 1; s <= n - k; s++) System.out.print(" ");
        for (int j = 1; j <= 2 * k - 1; j++)
            System.out.print(j == 1 || j == 2 * k - 1 ? "*" : " ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p10. The output above is copied from running it with n = 5.

Pattern 11: Solid square

The simplest nested loop: n rows, n columns, one star per cell.

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Row iLeading spaces = 0Items printed = n
105
205
305
405
505
static void p11(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) System.out.print("* ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p11. The output above is copied from running it with n = 5.

Number patterns (patterns 12–17, 26, 30)

Print the loop counter instead of a star.

Pattern 12: Number triangle (1, 12, 123)

The inner counter j itself is printed, so each row restarts at 1.

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Row iLeading spaces = 0Items printed = i
101
202
303
404
505
static void p12(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) System.out.print(j + " ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p12. The output above is copied from running it with n = 5.

Pattern 13: Same number on each row

The outer counter i is printed i times.

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Row iLeading spaces = 0Items printed = i
101
202
303
404
505
static void p13(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) System.out.print(i + " ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p13. The output above is copied from running it with n = 5.

Pattern 14: Inverted number triangle

The inner loop bound shrinks by one per row; the printed value is still j.

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Row iLeading spaces = 0Items printed = n βˆ’ i + 1
105
204
303
402
501
static void p14(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n - i + 1; j++) System.out.print(j + " ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p14. The output above is copied from running it with n = 5.

Pattern 15: Number pyramid (1, 121, 12321)

Two inner loops after the spaces: count up 1..i, then down iβˆ’1..1.

    1
   121
  12321
 1234321
123454321
Row iLeading spaces = n βˆ’ iCharacters printed = 2i βˆ’ 1
141
233
325
417
509
static void p15(int n) {
    for (int i = 1; i <= n; i++) {
        for (int s = 1; s <= n - i; s++) 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();
    }
}

Source: PatternPrograms.java, method p15. The output above is copied from running it with n = 5.

Pattern 16: Floyd’s triangle

One counter, next, lives outside both loops and never resets. Row i holds i consecutive numbers.

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Row iLeading spaces = 0Items printed = i
101
202
303
404
505
static void p16(int n) {
    int next = 1;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) System.out.print(next++ + " ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p16. The output above is copied from running it with n = 5.

Pattern 17: Pascal’s triangle

Each entry is C(r, k). Instead of computing factorials, step along the row with C(r, k+1) = C(r, k) Β· (r βˆ’ k) / (k + 1). The lead is 3 more than the shift because %4d right-aligns a single digit in four columns.

             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
   1   5  10  10   5   1
Row iLeading spaces = 2(n βˆ’ i) + 3Items printed = i
1131
2112
393
474
555
636
static void p17(int n) {
    for (int r = 0; r < n; r++) {
        for (int s = 0; s < n - 1 - r; s++) System.out.print("  ");
        int c = 1;                                   // C(r, 0)
        for (int k = 0; k <= r; k++) {
            System.out.printf("%4d", c);
            c = c * (r - k) / (k + 1);               // C(r, k+1) from C(r, k)
        }
        System.out.println();
    }
}

Source: PatternPrograms.java, method p17. The output above is copied from running it with n = 6.

Pattern 26: Palindromic number pyramid

Count down i..1, then up 2..i, so the row reads the same in both directions.

    1
   212
  32123
 4321234
543212345
Row iLeading spaces = n βˆ’ iCharacters printed = 2i βˆ’ 1
141
233
325
417
509
static void p26(int n) {
    for (int i = 1; i <= n; i++) {
        for (int s = 1; s <= n - i; s++) System.out.print(" ");
        for (int j = i; j >= 1; j--) System.out.print(j);
        for (int j = 2; j <= i; j++) System.out.print(j);
        System.out.println();
    }
}

Source: PatternPrograms.java, method p26. The output above is copied from running it with n = 5.

Pattern 30: Number diamond

The number pyramid, mirrored using the same k as the star diamond.

    1
   121
  12321
 1234321
123454321
 1234321
  12321
   121
    1
Row iLeading spaces = n βˆ’ kCharacters printed = 2k βˆ’ 1
141
233
325
417
509
617
725
833
941
static void p30(int n) {
    for (int i = 1; i <= 2 * n - 1; i++) {
        int k = i <= n ? i : 2 * n - i;
        for (int s = 1; s <= n - k; s++) System.out.print(" ");
        for (int j = 1; j <= k; j++) System.out.print(j);
        for (int j = k - 1; j >= 1; j--) System.out.print(j);
        System.out.println();
    }
}

Source: PatternPrograms.java, method p30. The output above is copied from running it with n = 5.

Alphabet patterns (patterns 18–20)

Add a number to 'A' and cast to char.

Pattern 18: Alphabet triangle (A, AB, ABC)

(char) ('A' + j βˆ’ 1) turns the column number into a letter.

A
A B
A B C
A B C D
A B C D E
Row iLeading spaces = 0Items printed = i
101
202
303
404
505
static void p18(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) System.out.print((char) ('A' + j - 1) + " ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p18. The output above is copied from running it with n = 5.

Pattern 19: Same letter on each row

The row number picks the letter: (char) ('A' + i βˆ’ 1).

A
B B
C C C
D D D D
E E E E E
Row iLeading spaces = 0Items printed = i
101
202
303
404
505
static void p19(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) System.out.print((char) ('A' + i - 1) + " ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p19. The output above is copied from running it with n = 5.

Pattern 20: Alphabet pyramid (A, ABA, ABCBA)

The number pyramid with letters: up to i letters, then back down.

    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
Row iLeading spaces = n βˆ’ iCharacters printed = 2i βˆ’ 1
141
233
325
417
509
static void p20(int n) {
    for (int i = 1; i <= n; i++) {
        for (int s = 1; s <= n - i; s++) System.out.print(" ");
        for (int j = 1; j <= i; j++) System.out.print((char) ('A' + j - 1));
        for (int j = i - 1; j >= 1; j--) System.out.print((char) ('A' + j - 1));
        System.out.println();
    }
}

Source: PatternPrograms.java, method p20. The output above is copied from running it with n = 5.

Binary, butterfly, hourglass, X, checkerboard and plus (patterns 21–25, 28, 29)

Shapes that combine mirroring with per-cell conditions.

Pattern 21: Binary triangle (1, 01, 101)

Cell (i, j) prints (i + j + 1) % 2, so the digits alternate and every row ends in 1.

1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
Row iLeading spaces = 0Items printed = i
101
202
303
404
505
static void p21(int n) {
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= i; j++) System.out.print((i + j + 1) % 2 + " ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p21. The output above is copied from running it with n = 5.

Pattern 22: Butterfly

Left wing of k stars, a gap of 2(n βˆ’ k) spaces, right wing of k stars. k rises to n then falls.

*      *
**    **
***  ***
********
********
***  ***
**    **
*      *
Row iLeading spaces = 0Characters printed = 2k (stars on both wings)
102
204
306
408
508
606
704
802
static void p22(int n) {
    for (int i = 1; i <= 2 * n; i++) {
        int k = i <= n ? i : 2 * n + 1 - i;          // stars on each side
        for (int j = 1; j <= k; j++) System.out.print("*");
        for (int s = 1; s <= 2 * (n - k); s++) System.out.print(" ");
        for (int j = 1; j <= k; j++) System.out.print("*");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p22. The output above is copied from running it with n = 4.

Pattern 23: Hourglass

With m running n, nβˆ’1, … 1, … n, each row has n βˆ’ m spaces then 2m βˆ’ 1 stars.

*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********
Row iLeading spaces = n βˆ’ mCharacters printed = 2m βˆ’ 1
109
217
325
433
541
633
725
817
909
static void p23(int n) {
    for (int i = 1; i <= 2 * n - 1; i++) {
        int m = i <= n ? n - i + 1 : i - n + 1;      // n, n-1, ... 1, ... n
        for (int s = 1; s <= n - m; s++) System.out.print(" ");
        for (int j = 1; j <= 2 * m - 1; j++) System.out.print("*");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p23. The output above is copied from running it with n = 5.

Pattern 24: Right arrow (half diamond)

The diamond’s k without the leading spaces.

*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Row iLeading spaces = 0Items printed = k
101
202
303
404
505
604
703
802
901
static void p24(int n) {
    for (int i = 1; i <= 2 * n - 1; i++) {
        int k = i <= n ? i : 2 * n - i;
        for (int j = 1; j <= k; j++) System.out.print("* ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p24. The output above is copied from running it with n = 5.

Pattern 25: X pattern

A star where j == i (main diagonal) or j == n βˆ’ 1 βˆ’ i (anti-diagonal), using 0-based indexes. Needs an odd n for a clean centre.

*     *
 *   *
  * *
   *
  * *
 *   *
*     *
Row iLeading spaces = min(i βˆ’ 1, n βˆ’ i)Characters printed = 2, or 1 on the middle row
102
212
322
431
522
612
702
static void p25(int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            System.out.print(j == i || j == n - 1 - i ? "*" : " ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p25. The output above is copied from running it with n = 7.

Pattern 28: Checkerboard

(i + j) % 2 == 0 picks #, otherwise ..

# . # . #
. # . # .
# . # . #
. # . # .
# . # . #
Row iLeading spaces = 0Items printed = n
105
205
305
405
505
static void p28(int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) System.out.print((i + j) % 2 == 0 ? "# " : ". ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p28. The output above is copied from running it with n = 5.

Pattern 29: Plus sign

A star when the row is the middle row or the column is the middle column.

  *
  *
*****
  *
  *
Row iLeading spaces = 0 on the middle row, else n/2Characters printed = n on the middle row, else 1
121
221
305
421
521
static void p29(int n) {
    int mid = n / 2;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) System.out.print(i == mid || j == mid ? "*" : " ");
        System.out.println();
    }
}

Source: PatternPrograms.java, method p29. The output above is copied from running it with n = 5.

Spiral matrix (pattern 27)

The one pattern that is a 2D array, not a row-by-row print.

Pattern 27: Spiral matrix

Fill the outer ring, shrink four boundaries, repeat. See the table below.

  1  2  3  4  5
 16 17 18 19  6
 15 24 25 20  7
 14 23 22 21  8
 13 12 11 10  9
Layertop / bottom / left / rightCells filledNumbers
Outer0 / 4 / 0 / 4161 to 16
Middle1 / 3 / 1 / 3817 to 24
Centre2 / 2 / 2 / 2125
static void p27(int n) {
    int[][] a = new int[n][n];
    int top = 0, bottom = n - 1, left = 0, right = n - 1, v = 1;
    while (top <= bottom && left <= right) {
        for (int j = left; j <= right; j++) a[top][j] = v++;        // β†’ along the top
        top++;
        for (int i = top; i <= bottom; i++) a[i][right] = v++;      // ↓ down the right
        right--;
        if (top <= bottom) {
            for (int j = right; j >= left; j--) a[bottom][j] = v++; // ← along the bottom
            bottom--;
        }
        if (left <= right) {
            for (int i = bottom; i >= top; i--) a[i][left] = v++;   // ↑ up the left
            left++;
        }
    }
    for (int[] row : a) {
        for (int x : row) System.out.printf("%3d", x);
        System.out.println();
    }
}

Source: PatternPrograms.java, method p27. The output above is copied from running it with n = 5.

Common interview questions

Why are pattern programs always nested loops?

A pattern is two-dimensional: rows and columns. The outer loop picks the row and the inner loop picks the column (or the number of symbols). The time complexity is O(n²) because the total output grows with the square of n, which is the number of symbols you must print anyway.

How do you print a diamond with one set of loops?

Use rows 1..2n−1 and compute k = i ≤ n ? i : 2n − i. Then each row has n − k spaces and k stars. That is pattern 6 above.

What is the difference between print and println inside the loops?

print keeps the cursor on the same line; println ends the line. Inner loops use print. The single println() goes after the inner loops, once per row.

How do you print a pattern of letters?

Letters are numbers in char form. (char) ('A' + j − 1) gives A for j = 1, B for j = 2. Patterns 18 to 20 use this.

How does the Pascal’s triangle code avoid factorials?

It updates the entry with c = c * (r − k) / (k + 1), which turns C(r, k) into C(r, k+1). The multiplication comes before the division, so the division is always exact.

Common mistakes

  • Off-by-one in the inner bound. j <= i prints i symbols; j < i prints i − 1. Check row 1 first.
  • Putting println inside the inner loop. Every symbol lands on its own line.
  • Forgetting the spaces in pyramids: you get a right triangle instead.
  • Mixing 1-based and 0-based indexes in the X pattern. With 0-based i, the anti-diagonal is n − 1 − i, not n − i.
  • Hard-coding the size. Keep n as a parameter. The same method then prints any size, and the logic table stays valid.
  • Computing Pascal’s triangle from factorials. n! overflows int at 13 (see the factorial post), long before the triangle gets interesting.

FAQ

How do I print a pyramid pattern in Java?

Use an outer loop for rows 1 to n. In each row, print n − i spaces, then print the string "* " i times, then call println. Pattern 4 on this page shows the full code and output.

What is the time complexity of a pattern program?

O(n²) for an n-row pattern, because the number of symbols printed grows with the square of n.

How do I print Floyd’s triangle in Java?

Keep one counter outside both loops, start it at 1, and print and increment it in the inner loop. Row i prints i numbers. See pattern 16.

How do I print Pascal’s triangle without factorials?

Start each row with 1 and compute the next entry as c * (r − k) / (k + 1). See pattern 17.

Which Java version do I need?

Any version from 8. I ran the programs on OpenJDK 21.

Further reading

No Comments yet!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.