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 withjavac PatternPrograms.java, run all 30 withjava PatternProgramsor one pattern withjava 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.
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.
- Count the rows. That is the outer loop, usually
1..n, or1..2n−1for a diamond. - For one row, write down the leading spaces as a formula in
iandn. - Write down how many symbols the row prints and what each one is: a star,
j,ior a letter. - 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 |
|---|---|
| 1 | Right triangle of stars |
| 2 | Inverted right triangle |
| 3 | Right-aligned triangle |
| 4 | Pyramid |
| 5 | Inverted pyramid |
| 6 | Diamond |
| 7 | Hollow square |
| 8 | Hollow right triangle |
| 9 | Hollow pyramid |
| 10 | Hollow diamond |
| 11 | Solid square |
| 12 | Number triangle (1, 12, 123) |
| 13 | Same number on each row |
| 14 | Inverted number triangle |
| 15 | Number pyramid (1, 121, 12321) |
| # | Pattern |
|---|---|
| 16 | Floyd’s triangle |
| 17 | Pascal’s triangle |
| 18 | Alphabet triangle (A, AB, ABC) |
| 19 | Same letter on each row |
| 20 | Alphabet pyramid (A, ABA, ABCBA) |
| 21 | Binary triangle (1, 01, 101) |
| 22 | Butterfly |
| 23 | Hourglass |
| 24 | Right arrow (half diamond) |
| 25 | X pattern |
| 26 | Palindromic number pyramid |
| 27 | Spiral matrix |
| 28 | Checkerboard |
| 29 | Plus sign |
| 30 | Number 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 i | Leading spaces = 0 | Items printed = i |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 0 | 2 |
| 3 | 0 | 3 |
| 4 | 0 | 4 |
| 5 | 0 | 5 |
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 i | Leading spaces = 0 | Items printed = n β i + 1 |
|---|---|---|
| 1 | 0 | 5 |
| 2 | 0 | 4 |
| 3 | 0 | 3 |
| 4 | 0 | 2 |
| 5 | 0 | 1 |
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 i | Leading spaces = 2(n β i) | Items printed = i |
|---|---|---|
| 1 | 8 | 1 |
| 2 | 6 | 2 |
| 3 | 4 | 3 |
| 4 | 2 | 4 |
| 5 | 0 | 5 |
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 i | Leading spaces = n β i | Items printed = i |
|---|---|---|
| 1 | 4 | 1 |
| 2 | 3 | 2 |
| 3 | 2 | 3 |
| 4 | 1 | 4 |
| 5 | 0 | 5 |
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 i | Leading spaces = i β 1 | Items printed = n β i + 1 |
|---|---|---|
| 1 | 0 | 5 |
| 2 | 1 | 4 |
| 3 | 2 | 3 |
| 4 | 3 | 2 |
| 5 | 4 | 1 |
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 i | Leading spaces = n β k | Items printed = k |
|---|---|---|
| 1 | 4 | 1 |
| 2 | 3 | 2 |
| 3 | 2 | 3 |
| 4 | 1 | 4 |
| 5 | 0 | 5 |
| 6 | 1 | 4 |
| 7 | 2 | 3 |
| 8 | 3 | 2 |
| 9 | 4 | 1 |
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 i | Leading spaces = 0 | Items printed = n on rows 1 and n, otherwise 2 |
|---|---|---|
| 1 | 0 | 5 |
| 2 | 0 | 2 |
| 3 | 0 | 2 |
| 4 | 0 | 2 |
| 5 | 0 | 5 |
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 i | Leading spaces = 0 | Items printed = 1, then 2, then n on the last row |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 0 | 2 |
| 3 | 0 | 2 |
| 4 | 0 | 2 |
| 5 | 0 | 5 |
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 i | Leading spaces = n β i | Characters printed = 1, then 2, then 2n β 1 on the last row |
|---|---|---|
| 1 | 4 | 1 |
| 2 | 3 | 2 |
| 3 | 2 | 2 |
| 4 | 1 | 2 |
| 5 | 0 | 9 |
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 i | Leading spaces = n β k | Characters printed = 1 at the tips, otherwise 2 |
|---|---|---|
| 1 | 4 | 1 |
| 2 | 3 | 2 |
| 3 | 2 | 2 |
| 4 | 1 | 2 |
| 5 | 0 | 2 |
| 6 | 1 | 2 |
| 7 | 2 | 2 |
| 8 | 3 | 2 |
| 9 | 4 | 1 |
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 i | Leading spaces = 0 | Items printed = n |
|---|---|---|
| 1 | 0 | 5 |
| 2 | 0 | 5 |
| 3 | 0 | 5 |
| 4 | 0 | 5 |
| 5 | 0 | 5 |
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 i | Leading spaces = 0 | Items printed = i |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 0 | 2 |
| 3 | 0 | 3 |
| 4 | 0 | 4 |
| 5 | 0 | 5 |
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 i | Leading spaces = 0 | Items printed = i |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 0 | 2 |
| 3 | 0 | 3 |
| 4 | 0 | 4 |
| 5 | 0 | 5 |
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 i | Leading spaces = 0 | Items printed = n β i + 1 |
|---|---|---|
| 1 | 0 | 5 |
| 2 | 0 | 4 |
| 3 | 0 | 3 |
| 4 | 0 | 2 |
| 5 | 0 | 1 |
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 i | Leading spaces = n β i | Characters printed = 2i β 1 |
|---|---|---|
| 1 | 4 | 1 |
| 2 | 3 | 3 |
| 3 | 2 | 5 |
| 4 | 1 | 7 |
| 5 | 0 | 9 |
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 i | Leading spaces = 0 | Items printed = i |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 0 | 2 |
| 3 | 0 | 3 |
| 4 | 0 | 4 |
| 5 | 0 | 5 |
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 i | Leading spaces = 2(n β i) + 3 | Items printed = i |
|---|---|---|
| 1 | 13 | 1 |
| 2 | 11 | 2 |
| 3 | 9 | 3 |
| 4 | 7 | 4 |
| 5 | 5 | 5 |
| 6 | 3 | 6 |
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 i | Leading spaces = n β i | Characters printed = 2i β 1 |
|---|---|---|
| 1 | 4 | 1 |
| 2 | 3 | 3 |
| 3 | 2 | 5 |
| 4 | 1 | 7 |
| 5 | 0 | 9 |
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 i | Leading spaces = n β k | Characters printed = 2k β 1 |
|---|---|---|
| 1 | 4 | 1 |
| 2 | 3 | 3 |
| 3 | 2 | 5 |
| 4 | 1 | 7 |
| 5 | 0 | 9 |
| 6 | 1 | 7 |
| 7 | 2 | 5 |
| 8 | 3 | 3 |
| 9 | 4 | 1 |
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 i | Leading spaces = 0 | Items printed = i |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 0 | 2 |
| 3 | 0 | 3 |
| 4 | 0 | 4 |
| 5 | 0 | 5 |
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 i | Leading spaces = 0 | Items printed = i |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 0 | 2 |
| 3 | 0 | 3 |
| 4 | 0 | 4 |
| 5 | 0 | 5 |
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 i | Leading spaces = n β i | Characters printed = 2i β 1 |
|---|---|---|
| 1 | 4 | 1 |
| 2 | 3 | 3 |
| 3 | 2 | 5 |
| 4 | 1 | 7 |
| 5 | 0 | 9 |
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 i | Leading spaces = 0 | Items printed = i |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 0 | 2 |
| 3 | 0 | 3 |
| 4 | 0 | 4 |
| 5 | 0 | 5 |
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 i | Leading spaces = 0 | Characters printed = 2k (stars on both wings) |
|---|---|---|
| 1 | 0 | 2 |
| 2 | 0 | 4 |
| 3 | 0 | 6 |
| 4 | 0 | 8 |
| 5 | 0 | 8 |
| 6 | 0 | 6 |
| 7 | 0 | 4 |
| 8 | 0 | 2 |
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 i | Leading spaces = n β m | Characters printed = 2m β 1 |
|---|---|---|
| 1 | 0 | 9 |
| 2 | 1 | 7 |
| 3 | 2 | 5 |
| 4 | 3 | 3 |
| 5 | 4 | 1 |
| 6 | 3 | 3 |
| 7 | 2 | 5 |
| 8 | 1 | 7 |
| 9 | 0 | 9 |
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 i | Leading spaces = 0 | Items printed = k |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 0 | 2 |
| 3 | 0 | 3 |
| 4 | 0 | 4 |
| 5 | 0 | 5 |
| 6 | 0 | 4 |
| 7 | 0 | 3 |
| 8 | 0 | 2 |
| 9 | 0 | 1 |
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 i | Leading spaces = min(i β 1, n β i) | Characters printed = 2, or 1 on the middle row |
|---|---|---|
| 1 | 0 | 2 |
| 2 | 1 | 2 |
| 3 | 2 | 2 |
| 4 | 3 | 1 |
| 5 | 2 | 2 |
| 6 | 1 | 2 |
| 7 | 0 | 2 |
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 i | Leading spaces = 0 | Items printed = n |
|---|---|---|
| 1 | 0 | 5 |
| 2 | 0 | 5 |
| 3 | 0 | 5 |
| 4 | 0 | 5 |
| 5 | 0 | 5 |
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 i | Leading spaces = 0 on the middle row, else n/2 | Characters printed = n on the middle row, else 1 |
|---|---|---|
| 1 | 2 | 1 |
| 2 | 2 | 1 |
| 3 | 0 | 5 |
| 4 | 2 | 1 |
| 5 | 2 | 1 |
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| Layer | top / bottom / left / right | Cells filled | Numbers |
|---|---|---|---|
| Outer | 0 / 4 / 0 / 4 | 16 | 1 to 16 |
| Middle | 1 / 3 / 1 / 3 | 8 | 17 to 24 |
| Centre | 2 / 2 / 2 / 2 | 1 | 25 |
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 <= iprintsisymbols;j < iprintsi − 1. Check row 1 first. - Putting
printlninside 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 isn − 1 − i, notn − i. - Hard-coding the size. Keep
nas a parameter. The same method then prints any size, and the logic table stays valid. - Computing Pascal’s triangle from factorials.
n!overflowsintat 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.
No Comments yet!