Decreasing & Increasing Alphabet Rows in Java

What You’ll Learn
Each row is made from two parts: a descending run from the row letter down to B, then an ascending run starting from A up to a computed cap so every row stays the same width.
The key formula is cap = startChar + endChar - i (which becomes 134 - i in the ASCII version for A…E).
⭐ Pattern Output
Output for A…E (no spaces):
ABCDE
BABCD
CBABC
DCBAB
EDCBAJava Program (Reference Logic, ASCII bound)
This uses the same bound as the classic sample: k <= 134 - i.
public class Main {
public static void main(String[] args) {
int i, j, k;
for (i = 65; i <= 69; i++) {
for (j = i; j > 65; j--)
System.out.print((char) j);
for (k = 65; k <= 134 - i; k++)
System.out.print((char) k);
System.out.println();
}
}
}🧠 How It Works
Outer i is the row anchor (ASCII)
i runs 65…69 (A…E). It is the leftmost high letter on the row; the second loop prints the ascending tail that keeps every line five characters wide.
Descending leg: j from i toward B
for (j = i; j > 65; --j) prints (char) j. When i == 65 the condition fails immediately, so row one is only the ascending half.
Ascending leg: k through 134 - i
for (k = 65; k <= 134 - i; ++k) is 65 + 69 - i in ASCII terms ('A' + 'E' - i). As i increases, the cap drops so the combined segments always print exactly five letters for this sample range.
Constant row length
Descending count is i - 65; ascending count is (134 - i) - 65 + 1 = 69 - i + 1. Their sum is 5 for every row.
Bow-tie pattern
Middle rows splice a descending prefix with an ascending tail. Two inner passes per row → O(n²) for n letters in the span.
Variation — User Input (no magic numbers)
Generalize using startChar and endChar so the cap becomes startChar + endChar - i.
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 > startChar; j--)
System.out.print(j);
for (char k = startChar; k <= (char) (startChar + endChar - i); k++)
System.out.print(k);
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Print a space after each character for readability
- Derive the cap as
endChar - (i - startChar)(equivalent form) - Validate
rowsto stay within A..Z
Avoid
- Using
2 * endChar - i(overshoots on the first row) - Letting the descending loop print
A(duplicates A at the join)
Key Takeaways
Cap formula: startChar + endChar - i (ASCII: 134 - i for A..E).
Descending loop stops before A to avoid duplicates.
Row length remains constant: endChar - startChar + 1.
O(n²) output for span n.
❓ Frequently Asked Questions
134 = 65 + 69 which is 'A' + 'E'. So 134 - i is just 'A' + 'E' - i.A is printed by the second loop. This prevents printing A twice.More Java alphabet patterns
Formulas like start + end - i help you avoid hard-coded bounds when generating symmetric strings.
The second loop prints (A + E - i) - A + 1 = E - i + 1 letters, which perfectly complements the i - A letters from the first loop to keep every row length constant.
12 people found this page helpful
