Right-aligned Descending Letters

Beginner
⏱️ 8 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Blanks + reverse scan

What You'll Learn

This pattern prints a right-aligned pyramid using descending letters per row. We scan from E down to A each time: if the current letter is “past” the row letter, we print a blank; otherwise we print the character.

⭐ Pattern Output

Output
    A
   BA
  CBA
 DCBA
EDCBA
1

Node.js / console version

Build each row as a string with leading spaces, then print it.

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

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

Browser version (document.write + div)

Use fixed-width cells so blanks don’t collapse.

HTML
<!DOCTYPE html>
<html>
<head>
  <style>
    div.cell {
      display: inline-block;
      text-align: center;
      width: 13px;
    }
  </style>
</head>
<body>
<script>
  const A = 65;
  const top = 69;
  for (let i = A; i <= top; i++) {
    for (let j = top; j >= A; j--) {
      if (j > i) document.write(\"<div class=\\\"cell\\\"></div>\");
      else document.write(\"<div class=\\\"cell\\\">\" + String.fromCharCode(j) + \"</div>\");
    }
    document.write(\"<br>\");
  }
</script>
</body>
</html>

🧠 How It Works

1

Outer loop: i from A to E

As i increases, one more letter becomes visible on the left.

5 rows
2

Inner loop: j from E to A

Each row scans five positions in reverse order (E..A).

Columns
3

j > i prints blanks

Before the diagonal, print spaces (or empty cells) to right-align the letters.

Alignment
=

Right-aligned triangle

Rows gradually fill from the rightmost A towards the leftmost E.

Key Takeaways

1

Scan letters in reverse (E..A) for each row.

2

Use a blank when j > i to right-align.

3

Fixed-width cells keep the browser output aligned.

4

Time complexity: O(n²).

❓ Frequently Asked Questions

Yes. Change top (for example to "J".charCodeAt(0) or 74) and the loops will scale automatically.
Multiple spaces collapse in HTML. Use &nbsp; or fixed-width cells to preserve alignment.
There are \(n\) rows and each row scans \(n\) letters: O(n²).

Keep going

Try the next alphabet program or browse the full list.

Next: Program 21 →
Did you know?

If you replace blanks with *, you’ll get an easy visual debug of alignment while building patterns.

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