Sequential Alphabet Triangle in Java

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Nested Loops

What You’ll Learn

Each row is wider than the last, but letters stay in order across the whole shape: A, then B C, then D E F, up to K L M N O for five rows. This differs from Program 1 (letters reset per row); here one counter walks the alphabet continuously. Includes a live preview, worked Java examples, edge cases, and complexity.

Shape Rule

Grow width, keep sequence

Row i prints i consecutive letters from a running counter.

Running Char

Never reset

char k = 'A'; lives outside the outer loop and advances across rows.

Inner Loop

Print then k++

Each cell prints k, optionally a space, then k++.

print vs println

Same line / next line

Letters use print; end each row with println().

Live Preview

1–7 rows

Pick a row count and draw the sequential triangle in the browser.

O(n²)

Complexity

Total letters = n(n+1)/2; extra memory stays O(1).

Introduction

A sequential alphabet triangle grows by one letter on each new line, but the alphabet does not restart. Letters flow continuously: the last letter on one row is followed by the next letter on the next row.

In Java you usually solve it with nested for loops plus a third variable k that increments after every print. Optional spaces between letters make the output easier to read.

Why it matters?

It is the classic “running counter” pattern. Once you can keep state across rows, many continuous fill patterns (letters, digits, or custom sequences) become straightforward.

Key Highlights

Continuous Letters

One k walks A, B, C… across the whole triangle.

Width Still Grows

Outer loop still prints 1, 2, 3, … letters per row.

Increment Per Cell

k++ belongs inside the inner loop, not after the row.

Not Program 1

Program 1 resets to A each row; this one never resets.

In short: start k = 'A', for each row print i letters with print(k) then k++, and call println() after the row.

📝 Problem & Approach

Given a positive integer rows, print a left-aligned triangle of consecutive alphabet letters where row i has i letters and the sequence never resets.

Java
// First 5 rows (with spaces)
// A
// B C
// D E F
// G H I J
// K L M N O

Inputs & Outputs

ItemTypeDescription
rowsintNumber of triangle lines. For A–Z only, keep rows(rows+1)/2 ≤ 26 (max 6 full rows, or 7 with overflow past Z).
Printed outputtextLeft-aligned consecutive letters; optional spaces between letters on a row.

Minimal workflow

Pseudocode
k = 'A'
for i from 1 to rows:
    for j from 1 to i:
        print k (no newline)
        if j < i: print space
        k = next letter
    print newline

Approach comparison

ApproachIdeaBest for
Running char kOuter width + inner print/k++Learning and interviews
Integer indexch = (char)('A' + count++)Same idea with an int counter

⚡ Quick Reference

GoalPattern
Start the sequencechar k = 'A'; (outside outer loop)
Grow row widthfor (int i = 1; i <= rows; i++)
Print next letterSystem.out.print(k); k++;
Space between lettersif (j < i) System.out.print(" ");
End the rowSystem.out.println();
Reset-per-row styleSee Program 1 (A, AB, ABC, …)

📋 print vs println vs k++

Same triangle — different roles for each tool.

System.out.print
same line

Prints a letter or space without moving to the next line

System.out.println
new line

Ends the current row after all letters are printed

k++
next letter

Advances the running character after each cell

Learning tip
no reset

Do not set k = 'A' inside the outer loop

Context

When This Pattern Shows Up

Reach for a running counter when values must continue across rows.

  1. After Program 1

    Contrast reset-per-row letters with a continuous sequence.

  2. State across loops

    Practice keeping mutable state outside the outer loop.

  3. Digit / token fills

    Same idea works with numbers or any ordered token stream.

  4. Gateway to Program 14

    Next: odd-length rows that still start from A each time.

  5. Not a UI layout tool

    This is a console teaching pattern — not how you build modern app screens.

Key benefit: one small program that locks in continuous state across nested loops — a skill used far beyond alphabet demos.

🔮 Live Preview

Choose a row count between 1 and 7 and draw the sequential alphabet triangle in the browser (spaces between letters).

Try 5 (through O) or 4 (through J). Six rows stay in A–Z; seven needs 28 letters (past Z).

Live result
Press "Draw pattern".

Examples Gallery

Three complete Java programs — fixed five rows, console input, and a compact no-space variant. Click View Output to reveal sample console results.

📚 Getting Started

Print five sequential rows with a running character and spaces.

Example 1 — Fixed rows = 5

Hard-coded height — ideal for first demos and screenshots.

Java
public class SequentialTriangle {
    public static void main(String[] args) {
        char k = 'A';

        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(k);
                if (j < i) System.out.print(" ");
                k++;
            }
            System.out.println();
        }
    }
}

How It Works

k starts at 'A' and never resets. Row 1 prints A, row 2 prints B C, and so on. k++ after each letter keeps the sequence continuous.

📈 Practical Variant

Let the user choose the height at runtime.

Example 2 — User Input Version

Read rows and keep printing. Check hasNextInt() in real apps; cap rows for A–Z if needed.

Java
import java.util.Scanner;

public class SequentialTriangleInput {
    public static void main(String[] args) {
        int rows;
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter the number of rows: ");
        rows = sc.nextInt();

        char k = 'A';
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(k);
                if (j < i) System.out.print(" ");
                k++;
            }
            System.out.println();
        }

        sc.close();
    }
}

How It Works

Same running-k core as Example 1; only the outer bound changes. For A–Z-only demos, stop when k > 'Z' or clamp so rows(rows+1)/2 ≤ 26.

⚡ Compact Style

Same sequence without spaces between letters.

Example 3 — No Spaces

Drop the space print for a denser triangle.

Java
public class SequentialTriangleCompact {
    public static void main(String[] args) {
        char k = 'A';

        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(k);
                k++;
            }
            System.out.println();
        }
    }
}

How It Works

The k++ logic is identical; only formatting changes. Spaces are readability sugar — they do not affect the letter sequence.

🧠 How the Algorithm Prints Rows

1

Set up

Import java.util.Scanner when reading input. Create char k = 'A'; before the outer loop.

Setup
2

Outer loop (width)

for (int i = 1; i <= rows; i++) decides how many letters this row prints.

1..n
3

Inner loop (cells)

Print k, optional space, then k++ so the next cell gets the next letter.

k++
4

New line

System.out.println() ends the row; k keeps its value for the next row.

Break
=

Triangle complete

Total letters: 1+2+…+n = n(n+1)/2O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — rows = 5

Trace each outer-loop value of i and watch how k advances across the whole triangle.

ik before rowPrinted rowk after row
1'A'A'B'
2'B'B C'D'
3'D'D E F'G'
4'G'G H I J'K'
5'K'K L M N O'P'

Total letter prints: 1 + 2 + 3 + 4 + 5 = 15 = 5×6/2 (A through O).

Use Cases

Where this tiny pattern (and its running counter) shows up beyond the homework prompt.

1. Continuous State Drill

Clearest demo that loop counters and printed values can be different variables.

Example: move k++ outside the inner loop and watch letters repeat.

2. Contrast with Program 1

Teach reset-per-row vs continuous-fill as a one-idea change.

Example: side-by-side A/AB/ABC vs A/BC/DEF.

3. Number Triangles

Swap char k for an int counter to print 1, 2 3, 4 5 6, …

Example: start n = 1 and print/increment the same way.

4. Formatting Variants

Add/remove spaces, or print commas, without changing the sequence logic.

Example: Example 3 drops spaces for a compact fill.

5. Complexity Intuition

Triangular totals make O(n²) concrete for beginners.

Example: 5 rows → 15 letters (A–O).

6. Alphabet Bounds Labs

Pair the pattern with a “stop at Z” or wrap policy.

Example: break when k > 'Z'.

Pro Tip: say “one counter walks the alphabet; the outer loop only chooses how many to print” before coding — that story prevents resetting k each row.

Advantages

Why this pattern earns a spot after the repeating-letter triangles.

  1. 1. Instant Visual Feedback

    Wrong increment placement shows up immediately as repeated letters.

  2. 2. Minimal Concepts

    Only loops, one extra char, and console output.

  3. 3. Easy to Adapt

    Swap letters for digits or remove spaces with tiny edits.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop counters and k.

Pro Tip: keep k outside the outer loop; resetting it each row accidentally recreates Program 1’s shape with a different letter rule.

Usage Tips

Small habits that keep sequential-pattern code clean.

  1. 1. Name the Runner

    Use k or nextLetter for the sequence — keep i/j for row/column.

  2. 2. Prefer hasNextInt()

    Avoid crashes when the user types letters instead of a number.

  3. 3. Increment Per Cell

    Put k++ inside the inner loop, after printing.

  4. 4. Watch the Alphabet Cap

    Six rows use 21 letters; seven need 28 — past Z unless you wrap/stop.

  5. 5. Dry-Run One Small n

    Trace rows = 3 (A / B C / D E F) on paper before coding larger demos.

Pro Tip: if every row starts with A, you almost certainly reset k inside the outer loop.

Common Pitfalls

Mistakes that commonly break sequential alphabet patterns.

  1. 1. Resetting k Each Row

    Setting k = 'A' inside the outer loop recreates a reset-style triangle.

    → Declare and initialize k once, before the outer loop.

  2. 2. Incrementing Only Once Per Row

    Moving k++ after the inner loop repeats the same letter across the row.

    → Increment inside the inner loop after each print.

  3. 3. println Inside the Inner Loop

    Each letter lands on its own line — you get a column, not a triangle.

    → Use print for letters; println only after the inner loop.

  4. 4. Blind nextInt()

    Letters or empty input throw InputMismatchException.

    → Check hasNextInt() and re-prompt on failure.

  5. 5. Ignoring the Z Boundary

    Large rows walk past 'Z' into non-letter characters.

    → Cap rows or stop when k > 'Z' for A–Z demos.

Edge Cases

Check these inputs before calling the solution done.

rows = 1

Single letter

Output is just A on one line.

rows = 0

Empty pattern

Treat as invalid; re-prompt instead of silent empty output.

rows = 6

Last full A–Z fit

21 letters (A–U). Still inside A–Z.

rows = 7

Past Z

28 letters needed — define wrap/stop policy.

Bad input

Non-numeric input

nextInt() throws — check hasNextInt().

Case

Lowercase variant

Same loops work with k = 'a'.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Compare with Program 1

  • Reset to A each row vs continuous k
  • See Program 1

2. Number version

  • Print 1 / 2 3 / 4 5 6 / …
  • Same loops, int counter

3. Stop at Z

  • Break when k > 'Z'
  • Partial last row is OK

4. Odd-length next

  • Continue to Program 14
  • Rows of length 1, 3, 5, …

Notes

  • Triangular count. Total letters for n rows is n(n+1)/2 — hence O(n²) time.
  • Keep k outside the outer loop; increment it inside the inner loop.
  • Spaces are optional formatting — they do not change the letter sequence.
  • Decide what happens past Z before accepting large row counts.

Quick Takeaway: outer loop picks the width, running k supplies consecutive letters, then break the line — that is the whole pattern.

⏱️ Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–3)O(rows²)O(1)
Wrap Up

🎉 Conclusion

The sequential alphabet triangle is a small nested-loop exercise with lasting payoff: continuous state across rows, print vs println, and O(n²) intuition. Master the running-k version, then optionally drop spaces for a compact fill.

Practice the three examples above, then continue to Program 14’s odd-length alphabet rows.

Keep k outside the outer loop, increment it per cell, and decide what happens after Z before accepting large row counts.

💡 Best Practices

✅ Do

  • Initialize k once before the outer loop
  • Increment k inside the inner loop after each print
  • Use System.out.print for letters and println after each row
  • Check hasNextInt() before calling nextInt()
  • State O(n²) time and the triangular letter count when asked

❌ Don’t

  • Reset k = 'A' on every outer iteration
  • Increment only once per row if you want per-cell sequences
  • Call println inside the inner letter loop
  • Ignore the Z boundary for large row counts
  • Confuse this with Program 1’s reset-per-row rule

Key Takeaways

Knowledge Unlocked

Five things to remember about this alphabet pattern

Print the sequential triangle the beginner-friendly way.

5
Core concepts
k 02

Running char

Never reset between rows

Code
++ 03

Inner loop

Print then k++

Code
04

println

Ends each row

I/O
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

Because k is updated after every printed character and is not reset inside the outer loop. That is what makes the sequence continuous across the triangle.
A single running character starts at A and increments after every print. Row 1 prints 1 character, row 2 prints 2, row 3 prints 3, so you see consecutive letters across the whole triangle.
Because each cell must print a new next letter. If you incremented only once per row, the wider rows would repeat the same letter.
System.out.print stays on the same line. System.out.println ends the current line. Letters (and optional spaces) use print; the row break uses println after the inner loop.
1+2+…+n = n(n+1)/2. For 5 rows that is 15 letters (A through O).
O(n²) where n is the number of rows. Total System.out.print calls for letters equal n(n+1)/2.
Plain char++ continues past Z into the next Unicode/ASCII values. Cap rows so n(n+1)/2 ≤ 26, or stop when k > 'Z', if you want A–Z only.
Check sc.hasNextInt() before sc.nextInt() and clamp rows so the triangular letter count stays in range for your alphabet policy.

Did you Know? 🔊

Unlike Program 1 (letters reset to A each row), this pattern uses one running character that increments after every print. Letters stay consecutive across the whole triangle: A, then B C, then D E F, and so on.

Continue to Alphabet Pattern 14

Next up: odd-length rows (A, ABC, ABCDE, …) stepping the end letter by two.

Program 14 tutorial →

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