Nearby lessons

28 of 125

Java - Pattern Programs

📌 What You Will Learn
  • 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.

In simple words: The outer loop prints the rows and the inner loop prints the columns. Change the condition or the print line inside the inner loop and you get a new pattern.
Example01
JCode Cell
1for (int i = 0; i < n; i++) { // outer loop -> rows
2 for (int j = 0; j < n; j++) { // inner loop -> columns
3 System.out.print("* "); // print without newline
4 }
5 System.out.println(); // after each row, go to next line
6}

Pattern 1 — Square of Stars (n x n)

Output for n = 5:

Example02
JCode Cell
1* * * * *
2* * * * *
3* * * * *
4* * * * *
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.

Example03
JCode Cell
1class Square {
2 public static void main(String[] args) {
3 int n = 5;
4 for (int i = 0; i < n; i++) { // rows
5 for (int j = 0; j < n; j++) { // columns
6 System.out.print("* ");
7 }
8 System.out.println();
9 }
10 }
11}

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.

Example04
JCode Cell
1*
2* *
3* * *
4* * * *
5* * * * *

Pattern 2 — Left Triangle of Stars

In simple words: `j <= i` makes the inner loop run `i + 1` times, so row 0 prints 1 star and row 4 prints 5 stars. This one condition turns a square into a triangle.
Example05
JCode Cell
1class LeftTriangle {
2 public static void main(String[] args) {
3 for (int i = 0; i < 5; i++) { // rows
4 for (int j = 0; j <= i; j++) { // up to i -> i+1 stars
5 System.out.print("* ");
6 }
7 System.out.println();
8 }
9 }
10}

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.

Example06
JCode Cell
1 *
2 * *
3 * * *
4 * * * *
5* * * * *

Pattern 3 — Right Triangle (with spaces)

Trainer's Note: The spacing in the output is the trickiest part of pattern programs. Print two spaces (" ") for the empty part so that the stars align as a neat triangle. If the alignment looks a little off in the terminal, do not worry — the logic is what matters.
Example07
JCode Cell
1class RightTriangle {
2 public static void main(String[] args) {
3 int n = 5;
4 for (int i = 0; i < n; i++) {
5 for (int s = 0; s < n - 1 - i; s++) // spaces
6 System.out.print(" ");
7 for (int j = 0; j <= i; j++) // stars
8 System.out.print("* ");
9 System.out.println();
10 }
11 }
12}

Pattern 4 — Pyramid

Example08
JCode Cell
1 *
2 * *
3 * * *
4 * * * *
5* * * * *

Pattern 4 — Pyramid

Example09
JCode Cell
1class Pyramid {
2 public static void main(String[] args) {
3 int n = 5;
4 for (int i = 0; i < n; i++) {
5 for (int s = 0; s < n - 1 - i; s++)
6 System.out.print(" "); // one space
7 for (int j = 0; j <= i; j++)
8 System.out.print("* "); // star + space
9 System.out.println();
10 }
11 }
12}

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.

Example10
JCode Cell
1* * * * *
2* * * *
3* * *
4* *
5*

Pattern 5 — Inverted Triangle

Example11
JCode Cell
1class InvertedTriangle {
2 public static void main(String[] args) {
3 int n = 5;
4 for (int i = 0; i < n; i++) {
5 for (int j = 0; j < n - i; j++) {
6 System.out.print("* ");
7 }
8 System.out.println();
9 }
10 }
11}

Number Patterns

To print numbers, just replace the star with a value that depends on the row or column.

Example12
JCode Cell
1class NumberPattern1 {
2 public static void main(String[] args) {
3 for (int i = 0; i < 5; i++) {
4 for (int j = 0; j <= i; j++) {
5 System.out.print((j + 1) + " "); // columns 1,2,3...
6 }
7 System.out.println();
8 }
9 }
10}
Output
1 1 2 1 2 3 1 2 3 4 1 2 3 4 5

Number Patterns

Example13
JCode Cell
1class NumberPattern2 {
2 public static void main(String[] args) {
3 int counter = 1;
4 for (int i = 0; i < 4; i++) {
5 for (int j = 0; j <= i; j++) {
6 System.out.print(counter + " ");
7 counter++; // 1,2,3,4,5...continuous
8 }
9 System.out.println();
10 }
11 }
12}
Output
1 2 3 4 5 6 7 8 9 10

Number Patterns

Example14
JCode Cell
1class NumberPattern3 {
2 public static void main(String[] args) {
3 for (int i = 0; i < 5; i++) {
4 for (int j = 0; j < 5; j++) {
5 System.out.print((i + 1) + " "); // same number in each row
6 }
7 System.out.println();
8 }
9 }
10}
Output
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5

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.

Example15
JCode Cell
1class FloydTriangle {
2 public static void main(String[] args) {
3 int n = 5, num = 1;
4 for (int i = 1; i <= n; i++) {
5 for (int j = 1; j <= i; j++) {
6 System.out.print(num + " ");
7 num++;
8 }
9 System.out.println();
10 }
11 }
12}
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Pattern Programs — Quick Comparison Table

PatternInner loop conditionAnything extra
Square (n x n)j < nNothing
Left trianglej <= iNothing
Right trianglej <= iSpaces loop: s < n-1-i
Pyramidj <= iSpaces loop (one space), star+space print
Inverted trianglej < n - iNothing
Number trianglej <= iPrint 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.
Trainer's Note: Exam tip: most pattern questions are just one line changed from the basic square. Change the inner condition from j < n to j <= i and a square becomes a triangle. Practise changing that one line on paper — it builds strong logic fast.

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 statementResult (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
Example18
JCode Cell
1class MatrixLetters {
2 public static void main(String[] args) {
3 for (int i = 0; i < 5; i++) {
4 for (int j = 0; j < 5; j++) {
5 System.out.print((char) (65 + j) + " "); // A B C D E
6 }
7 System.out.println();
8 }
9 }
10}
Output
A B C D E A B C D E A B C D E A B C D E A B C D E

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.

Example19
JCode Cell
1class Diamond {
2 public static void main(String[] args) {
3 int n = 5;
4 // top half (pyramid)
5 for (int i = 0; i < n; i++) {
6 for (int s = 0; s < n - 1 - i; s++) System.out.print(" ");
7 for (int k = 0; k < 2 * i + 1; k++) System.out.print("*");
8 System.out.println();
9 }
10 // bottom half (inverted pyramid)
11 for (int i = n - 2; i >= 0; i--) {
12 for (int s = 0; s < n - 1 - i; s++) System.out.print(" ");
13 for (int k = 0; k < 2 * i + 1; k++) System.out.print("*");
14 System.out.println();
15 }
16 }
17}
Output
* *** ***** ******* ********* ******* ***** *** *

Inverted (Hollow) Pyramids

For an inverted triangle of stars, the inner loop condition becomes j < n - i:

Example20
JCode Cell
1class InvertedPyramid {
2 public static void main(String[] args) {
3 int n = 5;
4 for (int i = 0; i < n; i++) {
5 for (int s = 0; s < i; s++) System.out.print(" ");
6 for (int k = 0; k < 2 * (n - i) - 1; k++) System.out.print("*");
7 System.out.println();
8 }
9 }
10}
Output
********* ******* ***** *** *
📝 Key Takeaways
  • 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.

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8