Square Number Triangle Pattern in C++

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:
1
4 9 16
25 36 49 64 81
100 121 144 169 196 225 256
289 324 361 400 441 484 529 576 625Complete 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).
#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
Track the square index
m starts at 1 and increases after each printed value. We print m*m.
Outer loop controls odd widths
i goes 1, 3, 5, 7, 9 using i += 2. That decides how many squares print on each row.
Print leading spaces for centering
The first inner loop prints spaces. As i grows, we print fewer spaces, shifting rows toward the left.
Print squares with fixed width
setw(4) reserves 4 characters for each number so multi-digit squares stay aligned.
Centered square triangle
The total printed values are 1+3+5+…+n, so runtime grows on the order of O(n²).
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.
#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
maxWidthto print more rows (keep it odd) - Use
setw(6)if you go beyond25^2or want more spacing - Print cubes (
m*m*m) instead of squares - Reset
meach 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
A counter m generates consecutive squares with m*m.
Row widths are odd numbers: 1, 3, 5, ... using i += 2.
Leading spaces plus setw() create a clean centered look.
Total printed values grow quadratically, so runtime is about O(n²).
❓ Frequently Asked Questions
i by 2 each time (i += 2), so it prints an odd count of values per row.m*m with m*m*m (and increase setw because cubes grow faster).Explore More C++ Number Patterns!
Try mixing math (squares, cubes) with alignment rules to create neat console designs.
The sum of the first n odd numbers is n². That’s why printing 1,3,5,7,9 values forms a very “square-friendly” growth pattern.
12 people found this page helpful
