Reverse Alphabet, Diagonal * in Java

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

What You’ll Learn

Five rows, each like EDCBA but with exactly one * where the row letter meets the column letter (i == j).

The star slides from the right (EDCB*) to the left (*DCBA) as i increases. Compare program 16 (centered pyramid, no diagonal).

⭐ Pattern Output

Output
EDCB*
EDC*A
ED*BA
E*CBA
*DCBA
1

Complete Java Program (A–E)

Outer i runs from 65 to 69 (A to E). Inner j runs from 69 down to 65 to print reverse letters across the row.

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

🧠 How It Works

1

Outer i

Row index A through E; also the value that will match j on the diagonal.

Rows
2

Inner j descends

Because j runs E down to A, each row would print EDCBA without the star condition.

EDCBA
3

if (i == j)

On the matching cell, print * instead of the letter.

Star
4

Row 1 walkthrough

i = 65 ('A'): j prints E, D, C, B; when j reaches A it equals i and becomes the star → EDCB*.

Example
=

Square grid

5 × 5 cells → 25 prints → O(n²) for n letters.

2

Variation — User Input

endChar = (char)('A' + rows - 1); outer i and inner j use char from 'A' through endChar. Clamp rows to 1–26.

Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        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++) {
            for (char j = endChar; j >= startChar; j--) {
                if (i == j) {
                    System.out.print("*");
                } else {
                    System.out.print(j);
                }
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Swap * for # or another marker
  • Mirror the inner loop to get ABCDE with a diagonal hole
  • Keep rows <= 26 so letters stay in A–Z

Avoid

  • Using rows larger than 26 without widening the alphabet range
  • Forgetting the reverse j loop (you would print ABCDE instead of EDCBA)

Key Takeaways

1

Descending j prints reverse letters on each line.

2

i == j picks one diagonal cell per row.

3

Generalize with endChar from rows and char loops.

4

O(n²) for an n×n letter square.

❓ Frequently Asked Questions

In the printed grid (rows top-to-bottom, columns left-to-right as j counts down), the stars sit on one anti-diagonal of the letter matrix.
Use a relation like i + j == startChar + endChar so the match moves along the opposite slant (adjust the range for your letters).
for (i = 65; i <= 69; i++) matches for (i = 'A'; i <= 'E'; i++) when i is int. Character literals are often easier to read.
O(n²) for n rows when each row has n columns.

More Java alphabet patterns

Pairing indices with character codes is a compact way to mark one cell per row.

All Alphabet Patterns →
Did you know?

System.out.print((char) j) and System.out.print(j) with char j both emit a single letter; pick one style and stay consistent.

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