Right-Aligned Floyd’s Triangle in C++

What You’ll Learn
How to print a right-aligned triangle of consecutive numbers in C++:
1, then 2 3, then 4 5 6, continuing until the last row.
We’ll use nested loops and setw() from <iomanip> to keep the triangle aligned.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15Complete C++ Program
The inner loop prints either spaces (for alignment) or the next number using setw(3).
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int rows = 5;
int i, j;
int k = 1;
for (i = 1; i <= rows; i++) {
for (j = rows; j >= 1; j--) {
if (j > i) {
cout << " ";
} else {
cout << setw(3) << k++;
}
}
cout << "\n";
}
return 0;
}🧠 How It Works
Initialize row count and counter
rows = 5 sets the triangle height and k = 1 is the next number to print.
Outer loop prints rows
for (i = 1; i <= rows; i++) controls how many numbers appear on each line (row i prints i numbers).
Inner loop handles alignment
for (j = rows; j >= 1; j--) prints either padding spaces (when j > i) or a number (otherwise).
setw(3) keeps columns neat
setw(3) reserves 3 characters for each number so 1-digit and 2-digit values align correctly.
Right-aligned Floyd’s triangle
Total printed numbers are 1+2+…+n = n(n+1)/2, so time complexity is O(n²) for n rows.
Variation — User Input Version
Let the user decide how many rows to print using cin:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int rows;
int i, j;
int k = 1;
cout << "Enter the number of rows: ";
cin >> rows;
for (i = 1; i <= rows; i++) {
for (j = rows; j >= 1; j--) {
if (j > i) {
cout << " ";
} else {
cout << setw(3) << k++;
}
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Increase
setw()when printing larger numbers (e.g., 100+) - Reset
keach row to print repeated-row patterns instead of consecutive numbers - Turn it into a left-aligned Floyd’s triangle by printing only the
inumbers and skipping the padding loop - Use
std::rightorstd::leftfor different alignment effects - Print the same triangle with a custom start value (initialize
kto something else)
Avoid
- Using
endlinside loops (it flushes output and slows printing) - Hard-coding the width if the pattern size can grow a lot
- Mixing alignment and value logic in the same loop without a clear condition
- Forgetting to increment
kafter printing a number
Key Takeaways
Use a counter k to print consecutive integers across rows.
Right alignment comes from printing spaces while j > i.
setw(3) keeps the triangle aligned for multi-digit values.
Total printed values are n(n+1)/2, giving O(n²) time.
❓ Frequently Asked Questions
setw(3). Printing " " keeps the padding width consistent with the number width.setw() (for example, use setw(4) or setw(5)) when numbers can reach 1000+.rows - i first, then print the i numbers. This example keeps alignment and printing in one inner loop.Explore More C++ Number Patterns!
Try combining alignment rules with different value formulas to build new triangle patterns.
setw() doesn’t change your number—it only changes how it is displayed. That makes it perfect for pattern printing where alignment matters.
12 people found this page helpful
