Diamond With Stars

Beginner
⏱️ 9 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Odd/even positions

What You'll Learn

This pattern prints a symmetric “diamond” using letters and stars. Each row uses the same letter (A, then B, then C, …) and inserts * between letters by checking whether the position is even or odd.

⭐ Pattern Output

Output
A
B*B
C*C*C
D*D*D*D
E*E*E*E*E
D*D*D*D
C*C*C
B*B
A
1

Node.js / console version

Print the top half (1..5), then the bottom half (4..1). Odd positions print the row letter; even positions print *.

JavaScript
const top = 5;

// Top half
for (let i = 1; i <= top; i++) {
  let line = "";
  for (let j = 1; j < i * 2; j++) {
    line += j % 2 === 0 ? "*" : String.fromCharCode(i + 64);
  }
  console.log(line);
}

// Bottom half
for (let i = top - 1; i >= 1; i--) {
  let line = "";
  for (let j = 1; j < i * 2; j++) {
    line += j % 2 === 0 ? "*" : String.fromCharCode(i + 64);
  }
  console.log(line);
}
2

Browser version (document.write)

Same logic as the reference, using document.write and line breaks.

HTML
<!DOCTYPE html>
<html>
<body>
<script>
  // Top half
  for (let i = 1; i <= 5; i++) {
    for (let j = 1; j < i * 2; j++) {
      if (j % 2 === 0) document.write("*");
      else document.write(String.fromCharCode(i + 64));
    }
    document.write("<br>");
  }

  // Bottom half
  for (let i = 4; i >= 1; i--) {
    for (let j = 1; j < i * 2; j++) {
      if (j % 2 === 0) document.write("*");
      else document.write(String.fromCharCode(i + 64));
    }
    document.write("<br>");
  }
</script>
</body>
</html>

🧠 How It Works

1

Top half: i from 1 to top

Row i prints 2i - 1 characters.

Grow
2

Inner loop: j from 1 to 2i - 1

Odd positions output the row letter (String.fromCharCode(i + 64)), even positions output *.

Alternate
3

Bottom half: i from top - 1 down to 1

Repeat the same alternating logic while shrinking the width to mirror the top half.

Mirror
=

Letter-star diamond

Two halves create a symmetric shape with * between repeated row letters.

Key Takeaways

1

Each row prints 2i - 1 characters.

2

Odd positions print letters; even positions print *.

3

Two halves (up + down) create the diamond.

4

Time complexity: O(n²).

❓ Frequently Asked Questions

Yes. Replace the "*" literal with "-" (or any character/string) in the even-position branch.
Change top (and the bottom loop start to top - 1). The letter is computed from i via String.fromCharCode(i + 64).
Across both halves, the total printed characters scale with \(1 + 3 + \u2026 + (2n-1)\), so the overall work is O(n²).

Keep going

Browse the full list of alphabet patterns for more loop practice.

Next: Program 22 →
Did you know?

String.fromCharCode(i + 64) maps 1→A, 2→B, 3→C, … (uppercase ASCII).

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