Square Number Triangle Pattern in C++

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

What You’ll Learn

How to print a centered triangle of square numbers in C++ using nested loops.

Each printed value is the square of a consecutive integer: 1, 4, 9, 16, ... and so on.

We’ll also use <iomanip> and setw() so the columns stay aligned.

⭐ Pattern Output

For a maximum width of 9 numbers on the last row, the pattern looks like this:

Output
                  1
              4   9  16
         25  36  49  64  81
    100 121 144 169 196 225 256
289 324 361 400 441 484 529 576 625
1

Complete C++ Program

We print odd row widths (1, 3, 5, 7, 9). A counter m increases continuously, and we print m*m with setw(4).

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

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

    for (i = 1; i <= 9; i += 2) {
        for (j = i; j < 9; j++)
            cout << "  ";

        for (k = 1; k <= i; k++) {
            cout << setw(4) << m * m;
            m++;
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Track the square index

m starts at 1 and increases after each printed value. We print m*m.

Squares
2

Outer loop controls odd widths

i goes 1, 3, 5, 7, 9 using i += 2. That decides how many squares print on each row.

Row width
3

Print leading spaces for centering

The first inner loop prints spaces. As i grows, we print fewer spaces, shifting rows toward the left.

Alignment
4

Print squares with fixed width

setw(4) reserves 4 characters for each number so multi-digit squares stay aligned.

Formatting
=

Centered square triangle

The total printed values are 1+3+5+…+n, so runtime grows on the order of O(n²).

2

Variation — User Input Version

Let the user choose the maximum odd width (example: 9). If the user enters an even number, we reduce it by 1 to make it odd.

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

int main() {
    int maxWidth;
    cout << "Enter the maximum odd width (e.g., 9): ";
    cin >> maxWidth;

    if (maxWidth < 1) return 0;
    if (maxWidth % 2 == 0) maxWidth--;

    int m = 1;
    for (int i = 1; i <= maxWidth; i += 2) {
        for (int j = i; j < maxWidth; j++)
            cout << "  ";

        for (int k = 1; k <= i; k++) {
            cout << setw(4) << m * m;
            m++;
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Increase maxWidth to print more rows (keep it odd)
  • Use setw(6) if you go beyond 25^2 or want more spacing
  • Print cubes (m*m*m) instead of squares
  • Reset m each row to print row-wise squares (1,4,9... per row)
  • Validate input and handle cin.fail() for robustness

Avoid

  • Dropping setw() if you want clean alignment
  • Using huge widths without adjusting spacing and field width
  • Mixing tab characters with spaces (alignment becomes inconsistent)
  • Forgetting the newline after each row

Key Takeaways

1

A counter m generates consecutive squares with m*m.

2

Row widths are odd numbers: 1, 3, 5, ... using i += 2.

3

Leading spaces plus setw() create a clean centered look.

4

Total printed values grow quadratically, so runtime is about O(n²).

❓ Frequently Asked Questions

Because the outer loop increases i by 2 each time (i += 2), so it prints an odd count of values per row.
It keeps the columns aligned as squares become multi-digit numbers, improving readability.
Replace m*m with m*m*m (and increase setw because cubes grow faster).
O(n²) in the size of the pattern because the total prints are 1+3+5+…+n.

Explore More C++ Number Patterns!

Try mixing math (squares, cubes) with alignment rules to create neat console designs.

All Number Patterns →
Did you know?

The sum of the first n odd numbers is . That’s why printing 1,3,5,7,9 values forms a very “square-friendly” growth pattern.

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