Repeating-Letter Alphabet Triangle in Java

What You’ll Learn
Row 1 prints one A, row 2 prints two Bs, row 3 prints three Cs, and so on: A, BB, CCC, DDDD, EEEEE.
The triangle geometry matches program 1, but instead of stepping letters inside the row, we repeat the same letter per row.
⭐ Pattern Output
When you run the program with rows = 5:
A
BB
CCC
DDDD
EEEEEComplete Java Program
Fixed rows = 5 version. Print the same letter i times on row i, then move to the next letter.
public class Main {
public static void main(String[] args) {
int rows = 5;
char ch = 'A';
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(ch);
}
System.out.println();
ch++;
}
}
}🧠 How It Works
Keep a row letter
ch stores the letter for the current row (A, then B, then C...).
Outer loop = row number
Outer loop runs i = 1 .. rows. Row i prints i characters.
Inner loop repeats the same letter
for (int j = 1; j <= i; j++) calls System.out.print(ch) with no change to ch inside the inner loop, so the entire row shows one repeated letter.
Next letter for the next row
System.out.println() ends the row; ch++ runs once per outer iteration so the following row uses the next alphabet letter (A, B, C, …).
Same triangle sizes, repeated glyphs
Row r still does r prints, so totals are 1 + 2 + … + n = n(n + 1)/2 — O(n²) time, O(1) extra space. Only the inner body differs from a stepping alphabet row.
Variation — User Input Version
Accept the number of rows using Scanner and clamp to 26 (A to Z):
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 (max 26): ");
int rows = sc.nextInt();
rows = Math.max(1, Math.min(rows, 26));
char ch = 'A';
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(ch);
}
System.out.println();
ch++;
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Compute the row letter as
(char)('A' + i - 1)instead of trackingch - Use lowercase letters by starting from
'a' - Try an inverted repeating triangle by printing fewer characters each row
- Center the triangle with leading spaces
Avoid
- Incrementing
chinside the inner loop (that changes letters across the row) - Allowing
rows> 26 without deciding how to handle letters after Z - Mixing this with program 1 logic where letters change within the row
Key Takeaways
Each row repeats a single letter; the repeat count equals the row number.
Advance the letter once per row (after the inner loop).
Total prints are \(n(n+1)/2\), so runtime is O(n²).
Small changes in the inner loop output create very different patterns.
❓ Frequently Asked Questions
'a' instead of 'A'.n rows.Next: Java Alphabet Pattern 10
Continue to Program 10 for the next alphabet pattern in Java.
You can also compute the row letter directly as (char)('A' + i - 1) each row instead of maintaining a separate ch variable.
10 people found this page helpful
