Rotating Alphabet Pattern in Java

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

What You’ll Learn

Each row is a cyclic rotation of the same set of letters. We print from the row start to the end, then wrap around by printing the earlier letters in reverse.

Output uses adjacent letters (no spaces), matching the reference.

⭐ Pattern Output

Output for 5 rows:

Output
ABCDE
BCDEA
CDEBA
DECBA
EDCBA
1

Java Program (Fixed A–E)

Forward run i..E plus wrap using (char)(k - 1) (same idea as System.out.format("%c", k - 1)).

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

🧠 How It Works

1

Outer i is the rotation start

Each row picks a new leading letter from A to E. The line is still a permutation of the same block AE, only rotated so a different letter leads.

Start
2

Forward leg: j from i to E

System.out.print((char) j) prints the suffix from the row start through the top letter (e.g. BCDE when i == 'B').

i..E
3

Wrap leg: k with (char)(k - 1)

for (k = i; k > 65; k--) prints (char)(k - 1), emitting i-1, i-2, … down to A without repeating the first letter of the forward segment.

k-1
4

Why every row has five letters

Forward part length 'E' - i + 1 plus wrap part i - 'A' sums to 'E' - 'A' + 1 for this fixed alphabet slice.

5
=

Cyclic shifts

Each row is a rotation of ABCDE. Two inner passes per outer row → O(n²) prints for n letters in the block.

2

Variation — User Input

Generalize with rows so letters range Achar('A' + rows - 1). 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 = i; j <= endChar; j++) {
                System.out.print(j);
            }
            for (char k = i; k > startChar; k--) {
                System.out.print((char) (k - 1));
            }
            System.out.println();
        }

        sc.close();
    }
}

💡 Tips for Enhancement

Try These

  • Add a space after each letter for readability
  • Keep rows capped so endChar stays within AZ
  • Try an array + modulo index for the same rotation idea

Avoid

  • Printing k (not k - 1) in the wrap loop (duplicates the first letter)
  • Letting rows push past sensible letter ranges without validation

Key Takeaways

1

Forward: i..end, then wrap: (i-1)..start.

2

Using k - 1 prevents duplicating the row’s first letter.

3

Every row has the same length: endChar - startChar + 1.

4

O(n²) total output for n letters.

❓ Frequently Asked Questions

Because the first loop already printed i. The wrap should continue with the letter before i, not repeat i again.
When i = A, the wrap loop does not run, so the row is just ABCDE.
O(n²) for n letters because there are n rows each printing n characters.

More Java alphabet patterns

Cyclic shifts are a compact way to practice loop boundaries without arrays.

All Alphabet Patterns →
Did you know?

If you imagine A B C D E on a circle, each row reads the letters starting from a different point on that circle.

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