Diamond-Shaped Alphabet Pattern in Java

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

This pattern is a full diamond made by stacking the inverted V from program 33 and then mirroring it back upward.

The only extra trick: start the bottom half from D so the widest row (E) is printed only once.

⭐ Pattern Output

Output for A.. E:

Output
    A
   B B
  C   C
 D     D
E       E
 D     D
  C   C
   B B
    A
1

Java Program (Reference Logic)

Two phases with identical inner loops; second phase starts at D to avoid duplicating the E row.

Java
public class Main {
    public static void main(String[] args) {
        int i, j, k;
        for (i = 65; i <= 69; i++) {
            for (j = 69; j >= 65; j--) {
                if (i == j) System.out.print((char) j);
                else System.out.print(" ");
            }
            for (k = 66; k <= 69; k++) {
                if (i == k) System.out.print((char) k);
                else System.out.print(" ");
            }
            System.out.println();
        }
        for (i = 68; i >= 65; i--) {
            for (j = 69; j >= 65; j--) {
                if (i == j) System.out.print((char) j);
                else System.out.print(" ");
            }
            for (k = 66; k <= 69; k++) {
                if (i == k) System.out.print((char) k);
                else System.out.print(" ");
            }
            System.out.println();
        }
    }
}

🧠 How It Works

1

Top outer loop (grow the V)

i runs 6569 (AE). Each larger i places the letter farther out on both diagonals so the hollow shape opens toward the widest E row.

2

Two inner passes per row

First, j scans E down to A; only when i == j does System.out.print((char) j) run, else a space—this draws the left leg. Then k scans B through E with the same equality rule—this draws the right leg.

j, k
3

Bottom outer loop (shrink the V)

The second part runs i from 68 down to 65 (DA) with the same inner loops pasted again, rebuilding the arms in reverse so the diamond closes symmetrically.

4

Why the bottom starts at D

The top half already printed the single-wide E row. Starting i at 68 skips repeating that middle line while still mirroring every narrower row.

E
=

Nine rows, two scans each

Five growing rows plus four shrinking rows; every row runs two full passes over the letter ranges. That is O(n²) character decisions for n letters along each leg.

2

Variation — User Input

Enter rows (half height). Bottom half starts at endChar - 1 to avoid duplicating the center row.

Java
import java.util.Scanner;

public class Main {
    static void printRow(char i, char startChar, char endChar) {
        for (char j = endChar; j >= startChar; j--)
            System.out.print(i == j ? j : ' ');
        for (char k = (char) (startChar + 1); k <= endChar; k++)
            System.out.print(i == k ? k : ' ');
        System.out.println();
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows (half): ");
        int rows = sc.nextInt();

        rows = Math.max(1, Math.min(rows, 26));
        char startChar = 'A';
        char endChar = (char) ('A' + rows - 1);

        for (char i = startChar; i <= endChar; i++)
            printRow(i, startChar, endChar);
        for (char i = (char) (endChar - 1); i >= startChar; i--)
            printRow(i, startChar, endChar);

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Fill the interior between the legs for a solid diamond
  • Use two spaces per blank to stretch the diamond
  • Keep rows within 26 for A..Z

Avoid

  • Starting the bottom half at endChar (duplicates the center row)
  • Proportional fonts when you want perfect alignment

Key Takeaways

1

Diamond = top half + mirrored bottom half.

2

Start bottom half at endChar - 1 to avoid a duplicate center.

3

Rows and width both scale as \(2n-1\).

4

O(n²) time for n letters.

❓ Frequently Asked Questions

Because the second phase begins at endChar - 1, skipping the already-printed endChar row.
O(n²) for n letters.

More Java pattern tutorials

Once you can mirror a shape vertically, you can create diamonds, hourglasses, and many other symmetric patterns.

All Alphabet Patterns →
Did you know?

The same row-printing logic is reused for both halves—only the range of the outer loop changes to mirror the output.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful