Number Mountain Pattern (Increase then Decrease)

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Two-phase row build

What You’ll Learn

How to print a number “mountain” where each row starts at the row number, climbs up, then comes back down:

1, 232, 34543, 4567654, 567898765.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
232
34543
4567654
567898765
1

Complete JavaScript Program

We use a variable m that starts at i, increases for i steps, then decreases for i-1 steps to build a symmetric row.

JavaScript
const rows = 5;

for (let i = 1; i <= rows; i++) {
  let m = i;
  let line = "";

  for (let j = 1; j <= i; j++) {
    line += m;
    m++;
  }

  m -= 2;
  for (let k = 1; k < i; k++) {
    line += m;
    m--;
  }

  console.log(line);
}

🧠 How It Works

1

Loop through rows

i = 1..rows decides how wide each mountain row is.

Rows
2

Start from i

m = i means row 3 starts at 3, row 4 starts at 4, and so on.

Start
3

Print the increasing part

The first inner loop prints i values: m, m+1, ....

Increase
4

Step back and print decreasing part

We do m -= 2 to avoid repeating the peak, then print i-1 values while decrementing.

Decrease
=

Symmetric mountain row

Row i always forms a symmetric peak with length 2i-1.

2

Variation — Browser (document.write) Version

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

HTML
<!DOCTYPE html>
<html>
<body>
<script>
var rows = 5;
for (var i = 1; i <= rows; i++) {
  var m = i;
  for (var j = 1; j <= i; j++) document.write(m++);
  m = m - 2;
  for (var k = 1; k < i; k++) document.write(m--);
  document.write("<br>");
}
</script>
</body>
</html>

💡 Tips for Enhancement

Try These

  • Increase rows to generate a taller mountain
  • Add spaces between digits for readability (use line += m + " ")
  • Start from a custom base number instead of i
  • Mirror the whole triangle vertically to create a diamond

Avoid

  • Forgetting m -= 2 (you’ll repeat the peak value)
  • Printing i values on the second loop (it should be i-1)
  • Omitting the newline after each row

Key Takeaways

1

Row i has 2i - 1 digits.

2

The first loop prints the increasing part.

3

The second loop prints the decreasing part after stepping back.

4

Two-phase row construction is a common pattern-printing technique.

❓ Frequently Asked Questions

Row 4 starts at 4, increases for 4 digits (4 5 6 7), then decreases for 3 digits (6 5 4) to form 4567654.
Yes—append a space after each number and use trimEnd() when printing the final line.
Set m to a fixed value before the first loop (instead of m = i).
Remove the m -= 2 step and start decreasing from m - 1 so the peak prints twice.

Explore More JavaScript Number Patterns!

Try adding spaces and centering this mountain to turn it into a pyramid-style pattern.

All Number Patterns →
Did you know?

The total number of digits printed up to row n is \(1 + 3 + 5 + \dots + (2n-1) = n^2\).

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