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 digits 1..i on each row, but the outer loop steps by 2 so only odd widths appear.

Remember
Rule: for i = max..1 step −2, print 1..i

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

In C you solve it with for (i = max; i >= 1; i -= 2) and an inner loop that prints 1..i with printf("%d", j), then printf("\n") ends the row. Change the step to i-- if you want every width (Program 1 style).

How to Solve It

Outer loop steps by 2 for odd widths only. Inner loop prints 1..i. End each row with printf("\n").

MethodIdeaBest for
Step by 2Outer visits odd widths only; inner prints 1..iLearning loop steps, this pattern
Step by 1Same inner loop; outer uses i-- for every widthContrast / full descending triangle

Pseudocode

Pseudocode
for i from max down to 1 step −2:
    for j from 1 to i:
        print j (no newline)
    print newline

Cheat sheet

GoalPattern
Odd widths onlyfor (i = max; i >= 1; i -= 2)
Print digits 1..ifor (j = 1; j <= i; j++) printf("%d", j);
End the rowprintf("\n");
Force odd maxif (max % 2 == 0) max -= 1;
Every widthfor (i = max; i >= 1; i--)

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", j)Stays on the same lineEach digit j
printf("\n")Ends the current lineAfter the inner loop

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

Live Preview

Change the maximum width and the odd-length triangle updates instantly. Even values are adjusted down to the nearest odd number.

Whole numbers from 1 to 9. Even input becomes the next lower odd max (e.g. 6 → 5).

Live result max 7 · 16 digits
1234567
12345
123
1

Worked Walkthrough — max = 7

Trace each outer-loop value of i (stepping by 2) and the ascending digits printed.

iInner jPrinted rowDigits
71..712345677
51..5123455
31..31233
11..111

Total digits are 7+5+3+1 = 16 — the square ((max+1)/2)² for odd max.

C Programs

Three complete programs: fixed odd max, scanf input with odd forcing, and a step-by-1 contrast. Use View Output for sample results.

Example 1 — Fixed max = 7

Outer loop steps by 2 for odd widths; inner loop prints digits 1..i.

C
#include <stdio.h>

int main(void)
{
    int i, j;

    for (i = 7; i >= 1; i -= 2)
    {
        for (j = 1; j <= i; j++)
            printf("%d", j);

        printf("\n");
    }

    return 0;
}

How It Works

1. Step by 2. for (i = 7; i >= 1; i -= 2) yields 7, 5, 3, 1 — even widths never run.

2. Print digits. When i = 7, the inner loop prints 1234567; when i = 1, just 1.

3. Newline. printf("\n") after the inner loop starts the next shorter odd-length row.

Example 2 — User Input

Read the maximum with scanf; force it odd if the user enters an even number.

C
#include <stdio.h>

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

    printf("Enter an odd maximum (e.g., 9): ");
    if (scanf("%d", &max) != 1 || max < 1)
    {
        printf("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++)
            printf("%d", j);

        printf("\n");
    }

    return 0;
}

How It Works

1. Validate and force odd. Require scanf success; if (max % 2 == 0) max -= 1 turns 6 into 5.

2. Same nested loops. With max = 5, rows are 12345, 123, and 1.

3. Safer demos. Cap at single-digit max if you want a tight look:

Safer input
if (scanf("%d", &max) != 1 || max < 1 || max > 9)
{
    printf("Enter a whole number from 1 to 9.\n");
    return 1;
}

Example 3 — Step by 1 (Contrast)

Change i -= 2 to i-- to print every width — a full descending triangle.

C
#include <stdio.h>

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

    for (i = max; i >= 1; i--)
    {
        for (j = 1; j <= i; j++)
            printf("%d", j);

        printf("\n");
    }

    return 0;
}

How It Works

1. Every width. i-- visits 5, 4, 3, 2, 1 — including even lengths that Example 1 skips.

2. Same inner loop. Digits are still 1..i; only the outer step changed. Keep i -= 2 for the odd-only lesson.

Edge Cases & Pitfalls

Check these before calling the solution done.

wrong step

i-- instead of i -= 2

That prints every width (Example 3). Keep stepping by 2 for odd-only rows.

even max

Starting at an even maximum

With max = 6 and no adjustment, the first row is even-length. Force odd with max -= 1 when needed.

\n inside

printf("\n") inside the inner loop

That puts each digit on its own line. Print digits without a newline; call printf("\n") only after the row finishes.

max = 1

Single digit

Output is just 1 — a good sanity check.

max ≤ 0

Empty output

The outer loop never runs. Validate and prompt again for clearer UX.

multi-digit

max > 9

printf("%d", 10) prints two characters, so columns drift. Labs usually stick to odd max values up to 9.

Time and Space Complexity

ProgramTimeExtra space
Odd-only loops (Examples 1–2)O(n²)O(1)
Step by 1 (Example 3)O(n²)O(1)

Odd widths print 1+3+…+n = ((n+1)/2)² digits for odd n — still quadratic in the maximum width.

Key Takeaways

  • Rule: for odd i from max down to 1 stepping by 2, print 1..i.
  • Step matters: i -= 2 skips even widths; i-- prints every width.
  • printf vs newline: digits stay on the line; printf("\n") advances after each row.
  • Next step: Program 15 builds an alternating binary number triangle.

One line: for each odd width from max down to 1, print 1..i with printf, then printf("\n").

Frequently Asked Questions

The outer loop uses i -= 2, so it visits only odd widths: 7, 5, 3, 1. Even lengths like 6, 4, 2 are skipped.
Because i -= 2 skips even row lengths. After 1234567 (7 digits), the next row is 5 digits (12345), not 6.
printf("%d", j) stays on the same line. printf("\n") ends the current line. Digits use printf; the row break uses printf("\n") after the inner loop.
Change the outer loop step to i-- (or i -= 1). Then you print 7, 6, 5, 4, 3, 2, 1 — every width.
Program 13 alternates ascending/descending direction with i % 2 (12345, 4321, 123, 21, 1). Program 14 always prints 1..i but only for odd row lengths using i -= 2.
O(n²) where n is the maximum row width. You print about 1+3+5+…+n odd-width digits, which is still O(n²).
Subtract 1 to make it odd (e.g. if max % 2 == 0 then max -= 1) so the first row stays odd-length, or document that even input shifts the pattern.
Check scanf's return value: if (scanf("%d", &max) != 1) handle bad input. Unchecked scanf leaves max uninitialized on failure.
The outer loop never runs, so nothing is printed. Validate and prompt again if you want a clear user message.

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

Print 0s and 1s that flip across rows and columns.

Program 15 tutorial →

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