Beginner Logical Thinking Java

Pattern Programs in Java with Explanation

Print common number and alphabet patterns for n rows using nested loops.

  • TimeO(n^2) for each pattern
  • SpaceO(1)
  • Hints3
  • Read4 min
Quick answer

Pattern programs in Java use nested loops. The outer loop picks the row, the inner loops print the spaces and values of that row, and println() ends the line. To solve any pattern, write a small table of row number, spaces and values, then turn each column of the table into a formula.

Nested loopsRow and column logicCharacter arithmetic
1

Problem statement

Pattern programs in Java ask you to print a shape made of numbers, letters or symbols. Given n, print each pattern below with loops only, with no fixed strings for whole rows.

This page covers number and alphabet patterns. For stars, see star pattern programs. For the full diamond shape, see diamond pattern.

Input

An integer n, the number of rows.

Output

The pattern, one row per line.

Examples

Example 1
Inputn = 3, number triangle
Output1 / 1 2 / 1 2 3

Row i prints the numbers 1 to i. A slash marks a new line here.

Example 2
Inputn = 3, repeated letters
OutputA / B B / C C C

Row i prints the i-th letter i times.

Constraints

  • 1 <= n <= 9 for number patterns. Floyd's triangle goes past one digit from row 4
  • 1 <= n <= 26 for alphabet patterns
2

Think before you code

Reveal the questions to ask yourself first
  • How many rows are there? That is the outer loop.
  • In row i, how many spaces and how many values are printed?
  • Does the printed value depend on the row, the column, or a running counter?
3

Hints

Open one at a time — try again after each.
1 Hint 1
Number the rows from 1 and write what row 1, row 2 and row 3 print.
2 Hint 2
If all values in a row are equal, print something based on i. If they change across the row, use j.
3 Hint 3
For letters, add to a char: (char) ('A' + i) gives the letter at position i, counted from 0.
4

Approach

Reveal the step-by-step approach

Pattern programs in Java: tricks that work every time

1. Outer loop = rows. Inner loops = what one row prints.

2. Make a table. For the palindrome pyramid with n = 4:

Row i Spaces Numbers going up Numbers going down
1 3 1 none
2 2 1 2 1
3 1 1 2 3 2 1
Formula n - i 1 to i i - 1 to 1

3. Choose what to print:

What you see Print
Same value across a row i
Value grows across a row j
Value never restarts a counter outside both loops
0 and 1 alternate (i + j) % 2
Letters (char) ('A' + number)

4. Call println() after the inner loops, once per row.

If loops are new to you, start with loops in Java.

5

Dry run

Walk through the example step by step

Floyd's triangle, n = 3. The counter k starts at 1 and is never reset.

i | j values | printed | k after the row
--+----------+---------+----------------
1 | 1        | 1       | 2
2 | 1 2      | 2 3     | 4
3 | 1 2 3    | 4 5 6   | 7
6

Solution

Reveal the full Java solution
Solution.java
public class NumberPatterns {
    // 1. Number triangle
    static void triangle(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }

    // 2. Same number in a row
    static void repeated(int n) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i + " ");
            }
            System.out.println();
        }
    }

    // 3. Floyd's triangle
    static void floyd(int n) {
        int k = 1;
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(k++ + " ");
            }
            System.out.println();
        }
    }

    // 4. Palindrome number pyramid
    static void palindrome(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();
        }
    }

    public static void main(String[] args) {
        triangle(4);
        System.out.println("---");
        repeated(4);
        System.out.println("---");
        floyd(4);
        System.out.println("---");
        palindrome(4);
    }
}

Output:

1
1 2
1 2 3
1 2 3 4
---
1
2 2
3 3 3
4 4 4 4
---
1
2 3
4 5 6
7 8 9 10
---
      1
    1 2 1
  1 2 3 2 1
1 2 3 4 3 2 1

Pattern programs in Java: explanation of each one

  • Number triangle: values grow across the row, so print j.
  • Repeated number: all values in a row match, so print i.
  • Floyd's triangle: the count continues to the next row, so use k outside the loops.
  • Palindrome pyramid: three inner loops, one each for spaces, up and down. Each number takes two characters, so each gap is two spaces.

Pattern program in Java using for loop: letters and binary

public class LetterPatterns {
    public static void main(String[] args) {
        int n = 4;
        // 5. Letters across the row
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print((char) ('A' + j) + " ");
            }
            System.out.println();
        }
        System.out.println("---");
        // 6. Same letter in a row
        for (int i = 0; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                System.out.print((char) ('A' + i) + " ");
            }
            System.out.println();
        }
        System.out.println("---");
        // 7. Binary triangle
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print((i + j + 1) % 2 + " ");
            }
            System.out.println();
        }
        System.out.println("---");
        // 8. Inverted number triangle
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + " ");
            }
            System.out.println();
        }
    }
}
A
A B
A B C
A B C D
---
A
B B
C C C
D D D D
---
1
0 1
1 0 1
0 1 0 1
---
1 2 3 4
1 2 3
1 2
1

In the binary triangle the first value of row 1 must be 1. With i = 1, j = 1, (1 + 1 + 1) % 2 is 1, and it flips each time j moves by one.

Pattern program in Java: practice list

Try these without looking at the code above:

  1. Rows 4 3 2 1, 4 3 2, 4 3, 4.
  2. Rows 1, 2 4, 3 6 9, 4 8 12 16. Print i * j.
  3. Rows D C B A, D C B, D C, D.
  4. A 4 by 4 grid of 0 and 1 like a chess board.
  5. The Pascal triangle.

For more loop practice, use the core Java practice problems. To learn with a teacher, see the Java full stack course.

TimeO(n^2) for each pattern
SpaceO(1)
7

Review before the interview

Common mistakes

  • Calling println() inside the inner loop, which puts each value on its own line.
  • Resetting the counter inside the outer loop in Floyd's triangle.
  • Writing 'A' + j without the (char) cast, which prints a number such as 65.
  • Using one space of indent when each value is two characters wide.

Edge cases to test

  • n = 1 prints a single value.
  • n = 0 prints nothing.
  • Values of 10 or more break the alignment. Use printf with a fixed width.
  • More than 26 letters runs past Z.

Interview follow-ups

  • How would you keep columns aligned when numbers reach two digits?
  • Can you print Floyd's triangle with a formula and no counter?
  • How would you write one of these with a while loop?

Keep practising

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Chat with us