C++ Descending Number Triangle Pattern (Odd Length)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

An odd-length descending number triangle prints 1..i only for odd widths, stepping the outer loop by 2 — 1234567, 12345, 123, 1.

Remember
Rule: for i from max down to 1 step 2
        for j from 1 to i: print j

1234567
12345
123
1         ← max = 7 (odd widths only)

Follows the alternating zigzag triangle in Program 13; next is the alternating binary triangle in Program 15.

How to Solve It

Outer loop visits odd lengths with i -= 2. Inner loop always prints ascending digits 1..i.

MethodIdeaBest for
Step-by-2 outer loopOuter i = max..1 with i -= 2; inner 1..iLearning, interviews, exams
Full descending triangleChange step to i-- to include even widthsComparing loop steps side by side

Pseudocode

Pseudocode
for i from max down to 1 step 2:
    for j from 1 to i:
        print j
    print newline

Cheat sheet

GoalPattern
Odd lengths onlyfor (i = max; i >= 1; i -= 2)
Print 1..ifor (j = 1; j <= i; j++) cout << j;
End of rowcout << "\n";
Force odd maxif (max % 2 == 0) max -= 1;
Include even widthsfor (i = max; i >= 1; i--)

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << jStays on the same lineEach digit on the row
cout << "\n"Ends the current lineAfter the inner loop finishes

Print digits without a newline, then end the row once.

Live Preview

Change the odd maximum width and the pattern updates instantly — even values are adjusted down to the nearest odd width.

Whole numbers from 1 to 9. Prefer odd widths; even input is reduced by 1 for this demo.

Live result max = 7 · 16 digits
1234567
12345
123
1

Worked Walkthrough — max = 7

Trace how i -= 2 visits only odd lengths while the inner loop always prints 1..i.

iInner jPrints
71..71234567
51..512345
31..3123
11..11

Even widths 6, 4, 2 never appear. Digit total for odd n is ((n+1)/2)² → still O(n²).

C++ Programs

Three complete programs: fixed max = 7, cin input (forced odd), and a compact max = 5 demo. Use View Output to reveal sample results.

Example 1 — Fixed max = 7

Hard-coded odd maximum — outer loop steps by 2; inner loop prints 1..i.

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

int main()
{
    int max = 7;
    int i, j;

    for (i = max; i >= 1; i -= 2)
    {
        for (j = 1; j <= i; j++)
            cout << j;
        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer skips even widths. i visits 7, 5, 3, 1 because of i -= 2.

2. Inner prints ascending digits. j always runs from 1 to i on that row.

3. Newline. Call cout << "\n" only after the inner loop finishes the row.

Example 2 — User Input Max

Read max with cin, validate, force odd if needed, then run the same nested loops.

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

int main()
{
    int max;
    int i, j;

    cout << "Enter an odd maximum (e.g. 7): ";
    if (!(cin >> max) || max <= 0)
    {
        cout << "Please enter a positive integer.\n";
        return 1;
    }
    if (max % 2 == 0)
        max -= 1;

    for (i = max; i >= 1; i -= 2)
    {
        for (j = 1; j <= i; j++)
            cout << j;
        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject failed reads and non-positive values before printing.

2. Force odd. Even input loses one so the first row stays an odd width.

3. Same core. Outer step-by-2 / inner 1..i matches Example 1 — only max comes from the user.

Example 3 — Compact max = 5

Same structure with a smaller odd maximum — easy to confirm the skipped even widths.

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

int main()
{
    int max = 5;
    int i, j;

    for (i = max; i >= 1; i -= 2)
    {
        for (j = 1; j <= i; j++)
            cout << j;
        cout << "\n";
    }

    return 0;
}

How It Works

1. Three odd rows. i = 5 → 12345; i = 3 → 123; i = 1 → 1.

2. Trace on paper. If you use i-- instead, you also get 1234 and 12 — the even widths return.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for max = 7 or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

i--

Even widths return

Using i-- instead of i -= 2 prints every length. Keep the step of 2 for odd-only rows.

even max

First row is even

Starting at 8 with i -= 2 yields even widths. Force odd first: if (max % 2 == 0) max -= 1;.

\n inside

Broken rows

If cout << "\n" sits inside the inner loop, each digit lands on its own line. Call it only after the row finishes.

max = 1

Single digit

Output is just 1 — a good sanity check for input validation.

max > 9

Multi-digit width

Digits 10+ make tight rows hard to read. Cap demos at 9 or switch to spaced cout << j << " ".

cin

Check the stream

Validate cin >> max before looping — a failed read leaves max unset or stale.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact max = 5 (Example 3)O(n²)O(1)

For odd n, digits printed = 1 + 3 + … + n = ((n+1)/2)² → still O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Step by 2: outer i -= 2 visits only odd row lengths.
  • Always 1..i: inner loop prints ascending digits on every odd-width row.
  • Break the row: cout << j in the inner loop; cout << "\n" after it.
  • Complexity: O(n²) time; O(1) extra space. Prefer an odd max.

One line: for each odd i from max down to 1, print 1..i, then newline.

Frequently Asked Questions

An odd-length descending triangle: for max=7 you get 1234567, 12345, 123, 1 — even widths like 6, 4, 2 are skipped.
The outer loop uses i -= 2, so it visits only odd widths: 7, 5, 3, 1.
Because i -= 2 skips even row lengths. After 1234567 (7 digits), the next row is 5 digits (12345), not 6.
Program 13 alternates ascending/descending with i % 2. Program 14 always prints 1..i but only for odd row lengths using i -= 2.
Change the outer loop step to i--. Then you print every width: 7, 6, 5, 4, 3, 2, 1.
cout << j stays on the same line. cout << "\n" ends the current line. Digits use cout << j; the row break uses cout << "\n" after the inner loop.
Force it odd first: if (max % 2 == 0) max -= 1; so the first row stays odd-length.
O(n²) for maximum width n. You print about 1+3+5+…+n digits, which is still O(n²).

Did you know?

Only odd-length rows print. The outer loop uses i −= 2 (7, 5, 3, 1) and the inner loop prints 1..i — still O(n²) total digit prints for maximum width n.

Next: Alternating Binary Triangle

Continue with rows of alternating 0 and 1 that grow each line.

Program 15 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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