Consecutive Letters (Shrinking Rows)

Beginner
⏱️ 9 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Global k counter

What You'll Learn

This pattern prints consecutive letters across rows while each row gets shorter. We keep a global counter k that starts at A, then print 5 letters on the first row, 4 on the second, and so on until 1 letter on the last row.

⭐ Pattern Output

Output
A B C D E
F G H I
J K L
M N
O
1

Node.js / console version

Use a global k and print letters with spaces. The inner loop length shrinks each row.

JavaScript
const A = "A".charCodeAt(0);
const top = "E".charCodeAt(0);
let k = A;

for (let i = A; i <= top; i++) {
  let line = "";
  for (let j = top; j >= i; j--) {
    line += String.fromCharCode(k++) + " ";
  }
  console.log(line.trimEnd());
}
2

Browser version (document.write)

Matches the reference: write each letter plus a space, then a line break.

HTML
<!DOCTYPE html>
<html>
<body>
<script>
  const A = 65;
  const top = 69;
  let k = A;
  for (let i = A; i <= top; i++) {
    for (let j = top; j >= i; j--) {
      document.write(String.fromCharCode(k++) + " ");
    }
    document.write("<br>");
  }
</script>
</body>
</html>

🧠 How It Works

1

Core idea

We keep a global counter k starting at A (65). Outer i runs from A to E. Inner loop runs from E down to i, printing one letter per step and incrementing k. The row length shrinks by 1 each line, so we print 5+4+3+2+1 = 15 letters (A through O).

Logic
2

Loops + condition

An outer loop picks the row, inner loop(s) decide what to print (letter / star / blank) based on comparisons.

Nested loops
=

Connect it to the output

Compare each loop boundary with the pattern output above — each row corresponds to one outer iteration.

Key Takeaways

1

Use k++ to keep letters continuous across rows.

2

Row length shrinks because the inner loop runs from E down to i.

3

5+4+3+2+1 = 15 letters (A..O).

4

Time complexity: O(n²).

❓ Frequently Asked Questions

With 5 rows, the total printed letters are 15, so the last printed letter is the 15th letter starting from A: O.
Change top to "F".charCodeAt(0) (or 70). You’ll print \(6+5+4+3+2+1=21\) letters (A through U).
Yes. Remove the + " " part in both examples to print letters tightly.

More JavaScript alphabet patterns

Browse the full list for more variations.

Next: Program 26 →
Did you know?

The total letters printed after \(n\) rows of a shrinking triangle is \(n(n+1)/2\), which is why 5 rows give 15 letters.

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