Rotating Alphabet Pattern in Java

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:
ABCDE
BCDEA
CDEBA
DECBA
EDCBAJava Program (Fixed A–E)
Forward run i..E plus wrap using (char)(k - 1) (same idea as System.out.format("%c", k - 1)).
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
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 A…E, only rotated so a different letter leads.
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').
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.
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.
Cyclic shifts
Each row is a rotation of ABCDE. Two inner passes per outer row → O(n²) prints for n letters in the block.
Variation — User Input
Generalize with rows so letters range A…char('A' + rows - 1). Clamp rows to 1–26.
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
rowscapped soendCharstays withinA…Z - Try an array + modulo index for the same rotation idea
Avoid
- Printing
k(notk - 1) in the wrap loop (duplicates the first letter) - Letting
rowspush past sensible letter ranges without validation
Key Takeaways
Forward: i..end, then wrap: (i-1)..start.
Using k - 1 prevents duplicating the row’s first letter.
Every row has the same length: endChar - startChar + 1.
O(n²) total output for n letters.
❓ Frequently Asked Questions
i. The wrap should continue with the letter before i, not repeat i again.i = A, the wrap loop does not run, so the row is just ABCDE.More Java alphabet patterns
Cyclic shifts are a compact way to practice loop boundaries without arrays.
If you imagine A B C D E on a circle, each row reads the letters starting from a different point on that circle.
12 people found this page helpful
