Odd-Length Descending Number Triangle in C++

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

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:

Output
1234567
12345
123
1
1

Complete 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.

C++
#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

1

Set the starting odd length

int rows = 7; sets the first (widest) row length.

Setup
2

Outer loop (shrink by 2)

for (i = rows; i >= 1; i -= 2) produces odd lengths: 7, 5, 3, 1.

Row control
3

Inner loop (print 1..i)

for (j = 1; j <= i; j++) prints a simple ascending sequence for the current row length.

Number printing
4

New line

cout << "\\n"; finishes each row.

Line break
=

Odd-length triangle

Printed numbers are 1+3+5+…+n = ((n+1)/2)² for odd n, so the work grows quadratically.

2

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.

C++
#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 << j with cout << (j * 2 - 1) to print odd numbers only
  • Add spaces between values: cout << j << ' ';

Avoid

  • Forgetting the i -= 2 step (you’ll get every length, not just odd ones)
  • Using negative or zero input without validation
  • Using endl inside loops (unnecessary flushing)
  • Mixing row/column logic (outer controls length; inner prints values)

Key Takeaways

1

The outer loop counts down by 2 to generate odd lengths (7, 5, 3, 1).

2

The inner loop prints a simple ascending sequence 1..i per row.

3

This creates a triangle that shrinks faster than standard patterns (by 2 each line).

4

Starting from an odd number gives only odd row lengths; starting from even includes even lengths too.

❓ Frequently Asked Questions

Because the outer loop decreases i by 2 each iteration (i -= 2), so it visits 7, 5, 3, 1 instead of every number.
Use i-- instead of i -= 2 in the outer loop. That will include both even and odd lengths.
Yes. Convert it to odd first (for example: if (rows % 2 == 0) rows--;) and then run the i -= 2 loop.
O(n²) in terms of the maximum length n, because the work is proportional to 1+3+5+…+n.

Explore More C++ Number Patterns!

From triangles to pyramids—each pattern helps you think in rows and columns.

All Number Patterns →
Did you know?

The sum of the first \(k\) odd numbers is \(k^2\). That’s why 1+3+5+7 = 16.

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