Repeating-Letter Alphabet Triangle in Java

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

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:

Output
A
BB
CCC
DDDD
EEEEE
1

Complete Java Program

Fixed rows = 5 version. Print the same letter i times on row i, then move to the next letter.

Java
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

1

Keep a row letter

ch stores the letter for the current row (A, then B, then C...).

State
2

Outer loop = row number

Outer loop runs i = 1 .. rows. Row i prints i characters.

Outer
3

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.

Repeat
4

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, …).

Advance
=

Same triangle sizes, repeated glyphs

Row r still does r prints, so totals are 1 + 2 + … + n = n(n + 1)/2O(n²) time, O(1) extra space. Only the inner body differs from a stepping alphabet row.

2

Variation — User Input Version

Accept the number of rows using Scanner and clamp to 26 (A to Z):

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 (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 tracking ch
  • 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 ch inside 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

1

Each row repeats a single letter; the repeat count equals the row number.

2

Advance the letter once per row (after the inner loop).

3

Total prints are \(n(n+1)/2\), so runtime is O(n²).

4

Small changes in the inner loop output create very different patterns.

❓ Frequently Asked Questions

Outside. Incrementing inside changes the letter across the row; outside keeps the row uniform.
Yes. Start from 'a' instead of 'A'.
O(n²) for n rows.

Next: Java Alphabet Pattern 10

Continue to Program 10 for the next alphabet pattern in Java.

Program 10 →
Did you know?

You can also compute the row letter directly as (char)('A' + i - 1) each row instead of maintaining a separate ch variable.

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.

10 people found this page helpful