Concentric Number Pattern (5 to 1 to 5)

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Symmetry + max()

What You’ll Learn

How to print a symmetric sequence pattern where each row forms values like 5 4 3 2 1 2 3 4 5 at the bottom, and the top row is all 5.

You’ll learn a reusable trick: compute each cell using distance from the center and a simple max rule.

⭐ Pattern Output

For size = 5, the pattern looks like this:

Output
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
1

Complete JavaScript Program

We print size rows. Each row uses 2*size - 1 columns and the value is max(i, distanceFromCenter + 1).

JavaScript
const size = 5;
const cols = 2 * size - 1;
const center = size; // 1-based index of the middle column

for (let i = size; i >= 1; i--) {
  const row = [];

  for (let c = 1; c <= cols; c++) {
    const dist = Math.abs(center - c);
    row.push(Math.max(i, dist + 1));
  }

  console.log(row.join(" "));
}

🧠 How It Works

1

Set the size

size = 5 controls the largest value and the number of rows.

Setup
2

Compute width

cols = 2*size - 1 makes a symmetric row with a single center column.

Width
3

Rows go from 5 down to 1

The outer loop prints the top row as all 5s, then gradually allows smaller values as i decreases.

Rows
4

Distance from center decides the border value

dist = |center - c| is 0 at the center and grows outward. dist + 1 becomes 1..5.

Distance
=

max(i, dist+1) builds the row

The max rule keeps large numbers on the border and reveals smaller numbers toward the center on lower rows.

2

Variation — Browser (document.write) Version

Print the same pattern in an HTML page using document.write:

HTML
<!DOCTYPE html>
<html>
<body>
<script>
var size = 5;
var cols = 2 * size - 1;
var center = size;

for (var i = size; i >= 1; i--) {
  for (var c = 1; c <= cols; c++) {
    var dist = Math.abs(center - c);
    var val = Math.max(i, dist + 1);
    document.write(val + " ");
  }
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Change size to generate a larger pattern (e.g., 7 prints 13 columns)
  • Print the full square (not just the top-to-center half) by mirroring rows
  • Pad numbers (like String(val).padStart(2)) to keep alignment for 2-digit sizes
  • Replace numbers with letters to create concentric alphabet patterns

Avoid

  • Forgetting that width is 2*size - 1 (symmetry breaks)
  • Mixing 0-based and 1-based indices without adjusting the center formula
  • Leaving trailing spaces in console output if you later compare strings exactly

Key Takeaways

1

Use 2*size - 1 columns for perfect symmetry.

2

Distance from center controls the outer values.

3

Math.max(i, dist + 1) gives the correct number for each cell.

4

This idea generalizes to many square and concentric patterns.

❓ Frequently Asked Questions

On the first iteration, i is 5, so max(i, dist+1) is always 5.
Yes—print rows from 1..size and then size-1..1, or use a square formula based on min distance to any edge.
Use row.join("") in the console version, and print val without a trailing space in the browser version.
Pad each number (e.g., String(val).padStart(2)) and join with a space so multi-digit values don’t shift columns.

Explore More JavaScript Number Patterns!

Try turning this into a full concentric square, then experiment with different border rules and padding for alignment.

All Number Patterns →
Did you know?

Concentric patterns are essentially a distance problem. Once you can compute a distance to a center (or to edges), you can generate many squares, diamonds, and rings with tiny formulas.

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