00:00

Java Pattern Programs – Star, Number and Character Patterns

Pattern programs print shapes such as triangles, pyramids and diamonds using stars, numbers or letters. They are a classic way to master nested loops: the outer loop controls the rows, and inner loops control the spaces and symbols in each row. They are also popular questions in exams and beginner interviews.

This page first explains the technique with runnable star, number and character examples, then provides a gallery of 56 solved patterns, each linking to a full program with output.

1. How Pattern Programs Work

Every pattern follows the same recipe:

  1. Draw the pattern for a small size (say n = 5) and number the rows.
  2. For each row, count the leading spaces and the symbols, and express both as formulas of the row number (for a pyramid: n - row spaces and row stars).
  3. Write an outer loop over rows and inner loops (or repeat()) that print those counts, then a line break.
  4. For shapes with a top and bottom half, such as diamonds and sandglasses, print two halves or loop from -(n-1) to n-1 and use Math.abs.

2. Star Patterns

StarPatterns.java
public class StarPatterns {
    public static void main(String[] args) {
        int n = 5;

        System.out.println("Right triangle:");
        for (int row = 1; row <= n; row++) {            // outer loop: one line per row
            for (int col = 1; col <= row; col++) {      // inner loop: 'row' stars
                System.out.print("* ");
            }
            System.out.println();
        }

        System.out.println("Pyramid:");
        for (int row = 1; row <= n; row++) {
            System.out.print(" ".repeat(n - row));      // leading spaces shrink
            System.out.println("* ".repeat(row));       // stars grow
        }

        System.out.println("Diamond:");
        for (int row = -(n - 1); row <= n - 1; row++) { // rows -4..4: distance from the middle
            int stars = n - Math.abs(row);
            System.out.print(" ".repeat(n - stars));
            System.out.println("* ".repeat(stars));
        }
    }
}
Output
Right triangle:
* 
* * 
* * * 
* * * * 
* * * * * 
Pyramid:
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
Diamond:
    * 
   * * 
  * * * 
 * * * * 
* * * * * 
 * * * * 
  * * * 
   * * 
    *

String.repeat() (Java 11) replaces an inner loop when a row consists of the same text repeated.

3. Number Patterns

NumberPatterns.java
public class NumberPatterns {
    public static void main(String[] args) {
        int n = 5;

        System.out.println("Row numbers:");
        for (int row = 1; row <= n; row++) {
            for (int col = 1; col <= row; col++) System.out.print(col + " ");
            System.out.println();
        }

        System.out.println("Floyd's triangle:");
        int value = 1;
        for (int row = 1; row <= n; row++) {
            for (int col = 1; col <= row; col++) System.out.printf("%-3d", value++);
            System.out.println();
        }

        System.out.println("Pascal's triangle:");
        for (int row = 0; row < n; row++) {
            System.out.print(" ".repeat(n - row - 1));
            int c = 1;                                   // C(row, 0)
            for (int k = 0; k <= row; k++) {
                System.out.print(c + " ");
                c = c * (row - k) / (k + 1);             // C(row, k+1) from C(row, k)
            }
            System.out.println();
        }
    }
}
Output
Row numbers:
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
Floyd's triangle:
1  
2  3  
4  5  6  
7  8  9  10 
11 12 13 14 15 
Pascal's triangle:
    1 
   1 1 
  1 2 1 
 1 3 3 1 
1 4 6 4 1

Number patterns print either the column index, a running counter (Floyd's triangle) or a computed value (Pascal's triangle, using binomial coefficients).

4. Character Patterns

CharacterPatterns.java
public class CharacterPatterns {
    public static void main(String[] args) {
        int n = 5;

        System.out.println("Letters per row:");
        for (int row = 0; row < n; row++) {
            for (char ch = 'A'; ch <= 'A' + row; ch++) System.out.print(ch + " ");
            System.out.println();
        }

        System.out.println("Same letter per row:");
        for (int row = 0; row < n; row++) {
            char ch = (char) ('A' + row);
            System.out.println((ch + " ").repeat(row + 1));
        }

        System.out.println("Palindromic pyramid:");
        for (int row = 0; row < n; row++) {
            StringBuilder line = new StringBuilder(" ".repeat(n - row - 1));
            for (char ch = 'A'; ch <= 'A' + row; ch++) line.append(ch);
            for (char ch = (char) ('A' + row - 1); ch >= 'A'; ch--) line.append(ch);
            System.out.println(line);
        }
    }
}
Output
Letters per row:
A 
A B 
A B C 
A B C D 
A B C D E 
Same letter per row:
A 
B B 
C C C 
D D D D 
E E E E E 
Palindromic pyramid:
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA

Characters are numeric codes, so 'A' + row moves forward through the alphabet; cast back with (char) to print a letter.

Click a pattern to see its complete program and output.

8. Tips for Solving Any Pattern

  • Make a small table of row number, spaces and symbols; the formula usually becomes obvious.
  • Use print inside the row and println only at the end of each row.
  • For hollow (outline) patterns, print a symbol only on the borders: first or last column, or last row; print spaces elsewhere.
  • Read n from the user with Scanner so the same program prints any size.
  • Check odd and even sizes and n = 1 as edge cases.

9. Summary

  • Pattern programs use an outer loop for rows and inner loops for spaces and symbols.
  • Express spaces and symbol counts as formulas of the row number.
  • Star, number and character patterns share the same structure; only the printed value changes.
  • Practise with the 56 solved patterns above to master nested loops.

Related Tutorials