Odd-Length Descending Number Triangle in C++

What You’ll Learn
How to print a descending number triangle with odd-length rows in C++: 1234567, 12345, 123, 1.
The key idea is to reduce the row length by 2 each time using i -= 2.
⭐ Pattern Output
For rows = 7, the pattern looks like this:
1234567
12345
123
1Complete C++ Program
The outer loop counts down by 2 (7, 5, 3, 1). The inner loop prints numbers from 1 up to the current value of i.
#include <iostream>
using namespace std;
int main() {
int rows = 7;
int i, j;
for (i = rows; i >= 1; i -= 2) {
for (j = 1; j <= i; j++) {
cout << j;
}
cout << "\n";
}
return 0;
}🧠 How It Works
Set the starting odd length
int rows = 7; sets the first (widest) row length.
Outer loop (shrink by 2)
for (i = rows; i >= 1; i -= 2) produces odd lengths: 7, 5, 3, 1.
Inner loop (print 1..i)
for (j = 1; j <= i; j++) prints a simple ascending sequence for the current row length.
New line
cout << "\\n"; finishes each row.
Odd-length triangle
Printed numbers are 1+3+5+…+n = ((n+1)/2)² for odd n, so the work grows quadratically.
Variation — User Input Version
Let the user choose the starting length. If the input is even, we reduce it by 1 to make it odd.
#include <iostream>
using namespace std;
int main() {
int rows;
int i, j;
cout << "Enter the starting row length: ";
cin >> rows;
if (rows % 2 == 0) rows -= 1;
for (i = rows; i >= 1; i -= 2) {
for (j = 1; j <= i; j++) {
cout << j;
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Center-align the rows by printing leading spaces before each line
- Start from a larger odd number (9, 11, ...) for a bigger triangle
- Print repeated digits instead of 1..i to create a different pattern family
- Swap
cout << jwithcout << (j * 2 - 1)to print odd numbers only - Add spaces between values:
cout << j << ' ';
Avoid
- Forgetting the
i -= 2step (you’ll get every length, not just odd ones) - Using negative or zero input without validation
- Using
endlinside loops (unnecessary flushing) - Mixing row/column logic (outer controls length; inner prints values)
Key Takeaways
The outer loop counts down by 2 to generate odd lengths (7, 5, 3, 1).
The inner loop prints a simple ascending sequence 1..i per row.
This creates a triangle that shrinks faster than standard patterns (by 2 each line).
Starting from an odd number gives only odd row lengths; starting from even includes even lengths too.
❓ Frequently Asked Questions
i by 2 each iteration (i -= 2), so it visits 7, 5, 3, 1 instead of every number.i-- instead of i -= 2 in the outer loop. That will include both even and odd lengths.if (rows % 2 == 0) rows--;) and then run the i -= 2 loop.Explore More C++ Number Patterns!
From triangles to pyramids—each pattern helps you think in rows and columns.
The sum of the first \(k\) odd numbers is \(k^2\). That’s why 1+3+5+7 = 16.
12 people found this page helpful
