Centered Increasing Number Triangle in C++

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a centered triangle of increasing numbers in C++:

1, 2 3 4, 5 6 7 8 9

We print leading spaces for centering and keep a counter to continue numbers across rows.

⭐ Pattern Output

For the example shown, the output looks like this:

Output
    1
  2 3 4
5 6 7 8 9
1

Complete C++ Program

The outer loop controls row width (1, 3, 5). The inner loop prints spaces on the left and then consecutive numbers using k++.

C++
#include <iostream>
using namespace std;

int main() {
    int i, j;
    int k = 1;
    int rowsWidth = 5;

    for (i = 1; i <= rowsWidth; i += 2) {
        for (j = rowsWidth; j >= 1; j--) {
            if (j > i) cout << " ";
            else cout << k++ << " ";
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Initialize the counter

int k = 1; ensures the numbers keep increasing across rows.

Setup
2

Pick odd row widths

for (i = 1; i <= 5; i += 2) creates row widths 1, 3, and 5.

Row control
3

Print spaces then numbers

When j > i, we print a space to push output right. Otherwise we print k++ followed by a space.

Centering
=

Centered consecutive triangle

This approach prints a centered triangle while keeping the sequence continuous.

2

Variation — User Input Version

Let the user choose the bottom width (odd number). The triangle will be centered automatically.

C++
#include <iostream>
using namespace std;

int main() {
    int width;
    int i, j;
    int k = 1;

    cout << "Enter the bottom width (odd number): ";
    cin >> width;

    if (!cin || width <= 0) return 0;
    if (width % 2 == 0) width--; // make it odd
    if (width <= 0) return 0;

    for (i = 1; i <= width; i += 2) {
        for (j = width; j >= 1; j--) {
            if (j > i) cout << " ";
            else cout << k++ << " ";
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Use setw(2) or setw(3) (from <iomanip>) to keep alignment after 9
  • Remove trailing spaces by printing a space only between values
  • Start the counter from another value (e.g., k = 10) to shift the sequence
  • Print a full pyramid by continuing past the middle and then decreasing widths
  • Replace numbers with characters to build centered alphabet patterns

Avoid

  • Using even widths (convert to odd so centering stays symmetric)
  • Using endl in loops (unnecessary flushing)
  • Not validating cin input before using width
  • Forgetting the counter needs to stay outside the outer loop

Key Takeaways

1

Odd widths (1, 3, 5, ...) make it easy to center the triangle.

2

Leading spaces shift the numbers right to create centering.

3

A counter k keeps the number sequence continuous across rows.

4

This pattern can be extended into a full pyramid by adding decreasing widths.

❓ Frequently Asked Questions

Leading spaces push the row to the right so that shorter rows are centered above the widest row.
Because k is not reset inside the outer loop. It keeps incrementing as you print.
Use <iomanip> and print with setw(2) or setw(3) so all numbers use the same width.
O(n²) for \(n\) rows, because the pattern prints a triangular number of values.

Explore More C++ Number Patterns!

Centered triangles are great practice for combining spacing logic with counters.

All Number Patterns →
Did you know?

Many centered patterns work by printing spaces first and then the pattern content. Getting the spacing right is often the hardest part.

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