Nearby lessons
28 of 125Java - Pattern Programs
- The logic behind pattern printing
- How nested loops build patterns
- Star patterns and number patterns
- How to change a pattern by changing just one line
- Common pattern programs asked in exams and interviews
Pattern Programs is a core concept of the Java language. This lesson explains The Secret of All Patterns, Pattern 1 — Square of Stars (n x n) and Pattern 2 — Left Triangle of Stars with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
The Secret of All Patterns
Every pattern program is built from the same idea: two loops — an outer loop and an inner loop.
- The outer loop controls the number of rows.
- The inner loop controls what is printed in each row.
If you understand which loop prints rows and which loop prints columns, you can print any pattern in the world.
Pattern 1 — Square of Stars (n x n)
Output for n = 5:
Pattern 1 — Square of Stars (n x n)
Here both loops run from 0 to n, so every row has n stars — a perfect square.
Pattern 2 — Left Triangle of Stars
Observation: row 0 has 1 star, row 1 has 2 stars, row 2 has 3 stars... So in row i, the inner loop runs i + 1 times.
Pattern 2 — Left Triangle of Stars
Pattern 3 — Right Triangle (with spaces)
This pattern has two parts in every row: spaces first, then stars. For row i, we print (n-1-i) spaces and then (i+1) stars.
Pattern 3 — Right Triangle (with spaces)
Pattern 4 — Pyramid
Pattern 4 — Pyramid
Pattern 5 — Inverted Triangle
Row 0 has 5 stars, row 1 has 4 stars... row i has (n - i) stars. So the inner loop condition becomes j < n - i.
Pattern 5 — Inverted Triangle
Number Patterns
To print numbers, just replace the star with a value that depends on the row or column.
Number Patterns
Number Patterns
Pattern 6 — Floyd's Triangle
Floyd's triangle is a famous pattern where we print counting numbers in a triangle shape. It is a favourite question in placements.
Pattern Programs — Quick Comparison Table
| Pattern | Inner loop condition | Anything extra |
|---|---|---|
| Square (n x n) | j < n | Nothing |
| Left triangle | j <= i | Nothing |
| Right triangle | j <= i | Spaces loop: s < n-1-i |
| Pyramid | j <= i | Spaces loop (one space), star+space print |
| Inverted triangle | j < n - i | Nothing |
| Number triangle | j <= i | Print value of j+1 or a counter |
How to Build Any Pattern — Step Method
- Look at the pattern. Decide the number of rows → that is the outer loop limit.
- Ask: in row i, what values are printed? Find a formula using i and j.
- Ask: are there leading spaces? If yes, add a third loop for spaces before the value loop.
- Keep the inner print as print(...) (no newline) and add println() after every row.
- Test with a small value of n, then increase.
Pattern Practice Set — Matrix Value Variations
The classic material begins with a square of values, then asks you to change what is printed (the formula) while keeping the same two loops. Notice how each pattern changes only one print line:
| Print statement | Result (10x10 matrix) |
|---|---|
| print("* ") | Square of stars |
| print(j + " ") | Columns 0 to 9 in every row |
| print((9 - j) + " ") | Columns 9 down to 0 |
| print(i + " ") | Row number in every column |
| print((9 - i) + " ") | Row number reversed |
| print((char)(65 + j) + " ") | Letters A B C ... J |
| print((char)(74 - j) + " ") | Letters J I H ... A |
Diamond Patterns
A diamond = one pyramid + one inverted pyramid. Build the top half first, then the bottom half.
Look at the bottom loop: it runs i from n-2 down to 0, reusing the same 2*i+1 formula. That single reversal turns a pyramid into a diamond.
Inverted (Hollow) Pyramids
For an inverted triangle of stars, the inner loop condition becomes j < n - i:
- Every pattern = outer loop for rows + inner loop for columns.
- Print without newline (System.out.print) for columns; println() after each row.
- Row i of a left triangle needs i+1 elements: inner condition j <= i.
- Inverted shapes use condition j < n - i.
- Shapes with spaces need a separate spaces loop before the value loop.
- Number patterns replace '*' with formulas based on i and j.