C Palindrome Number Triangle Pattern

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

What Is This Pattern?

A palindrome number triangle grows one peak digit per row: print 1..i, then mirror i-1..1 on the same line — so each row reads the same forwards and backwards.

Remember
Rule: for i = 1..rows
      print 1..i, then i-1..1

1
121
12321
1234321
123454321     ← rows = 5

Two inner loops build the symmetry: ascend to the peak, then descend without repeating it. Follows the diagonal asterisk in Program 26; next is Program 28.

How to Solve It

Start with fixed rows and compact digits, then add scanf — optionally space the digits for easier reading.

MethodIdeaBest for
Ascend + descend loopsPrint 1..i, then i-1..1Learning, interviews, exams
Spaced digitsSame loops; printf("%d ", …)Easier reading once the core clicks

Pseudocode

Pseudocode
for i from 1 to rows:
    for j from 1 to i:
        print j (no newline)
    for k from i-1 down to 1:
        print k (no newline)
    print newline

Cheat sheet

GoalPattern
Grow each rowfor (i = 1; i <= rows; i++)
Left half 1..ifor (j = 1; j <= i; j++) printf("%d", j);
Mirror i-1..1for (k = i - 1; k >= 1; k--) printf("%d", k);
End the rowprintf("\n");
Spaced variantprintf("%d ", j); in both loops

Printing Numbers vs Starting a New Line

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

Print the whole palindrome without a newline, then end the row once.

Live Preview

Change the row count and the palindrome triangle updates instantly — capped at 9 so every digit stays a single character.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result 5 rows · 25 digits
1
121
12321
1234321
123454321

Worked Walkthrough — rows = 4

Trace each outer-loop value of i, the ascending half, and the mirror half.

iAscend jMirror kDigitsPrinted row
11(none)11
21 213121
31 2 32 1512321
41 2 3 43 2 171234321

Row i prints 2i - 1 digits. Total for n rows: 1 + 3 + … + (2n - 1) = n² — why time is O(n²).

C Programs

Three complete programs: fixed rows, scanf input, and a spaced-digits variant. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — ascend to the peak, then mirror without repeating it.

C
#include <stdio.h>

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

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

        for (k = i - 1; k >= 1; k--)
            printf("%d", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Outer loop picks the peak. i runs from 1 to 5 — the middle digit of that row.

2. Ascend 1..i. j prints the left half including the peak.

3. Mirror i-1..1. k starts at i - 1 so the peak is not printed twice.

4. Break the line. printf("\n") after both inner loops starts the next (longer) row.

When i = 3 you get 123 then 21 → 12321.

Example 2 — User Input Version

Read rows with scanf. Prefer checking its return value in real apps (shown in the tip below).

C
#include <stdio.h>

int main(void)
{
    int rows;
    int i, j, k;

    printf("Enter rows: ");
    scanf("%d", &rows);

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

        for (k = i - 1; k >= 1; k--)
            printf("%d", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and read. Ask for a row count, then store it with scanf("%d", &rows).

2. Same two-loop core. Only the outer bound changes from 5 to rows.

3. Safer input tip. Unchecked scanf leaves rows uninitialized on bad input. Prefer:

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

Example 3 — Spaced digits

Keep the same loops; print each digit followed by a space for easier reading.

C
#include <stdio.h>

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

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

        for (k = i - 1; k >= 1; k--)
            printf("%d ", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Ascend and mirror bounds are unchanged.

2. Add trailing spaces. Only the print statements change — printf("%d ", …) in both loops.

3. Learn the compact form first. Use Examples 1–2 for the classic no-space triangle; treat spacing as a polish option.

Edge Cases & Pitfalls

Check these before calling the solution done.

k = i

Doubled peak

If the mirror starts at i instead of i - 1, you get 123321 instead of 12321. Always start at i - 1.

no mirror

Left half only

Skipping the descending loop prints 1, 12, 123 — not palindromes. Add the k loop after the ascending loop.

\n inside

Column of digits

If printf("\n") sits inside either inner loop, each digit lands on its own line. Call the newline only after both loops.

rows > 9

Multi-digit numbers

printf("%d", 10) prints two characters, so the palindrome shape drifts. Cap demos at 9 or use fixed-width formatting.

rows = 1

Single digit

Output is just 1 — the mirror loop does not run. A good sanity check.

scanf

Check the return value

If scanf fails, rows may be uninitialized — always test scanf(...) == 1.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(rows²)O(1)
Spaced digits (Example 3)O(rows²)O(1)

Total digits = 1 + 3 + … + (2n - 1) = n² — quadratic in n.

Key Takeaways

  • Rule: print 1..i, then i-1..1 — peak appears once.
  • Two loops: ascend with j, mirror with k starting at i - 1.
  • Break the row: call printf("\n") only after both inner loops.
  • Complexity: O(n²) time from n² digits; O(1) extra space.

One line: for each i, print 1..i then i-1..1, then printf("\n").

Frequently Asked Questions

Starting from i-1 avoids repeating the middle number. For example, for i=3, printing 123 then 21 makes 12321.
Use printf("%d ", j) and printf("%d ", k) in the loops instead of printf("%d", j) — see Example 3.
The ascending loop prints 1..i and the descending loop prints i-1..1 — together they read the same forwards and backwards.
printf("%d", j) prints digits on the same line. printf("\n") ends the row after both inner loops finish.
One loop handles the ascending half (j) and the other handles the mirror half (k) — clearer than a single complex loop.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because row i prints 2i-1 digits and the total is 1+3+5+...+(2n-1) = n².
Check scanf's return value: if (scanf("%d", &rows) != 1) handle bad input. Unchecked scanf leaves rows uninitialized on failure.

Did you know?

This palindrome triangle prints 1..i and then i-1..1 on each row. The second loop mirrors the first without repeating the peak, producing outputs like 12321 and 123454321.

Next: 0-Centered Descending Mirror

Print a descending mirror with a zero in the middle of each row.

Program 28 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