C Palindrome Number Triangle Pattern (Outer Peak)

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

What Is This Pattern?

A palindrome number triangle prints a mirrored sequence on each row: descend from i to 2, then ascend from 1 to i. Every row reads the same both ways.

Remember
Rule: for i from 1 to rows
      print j from i down to 2
      print j from 1 up to i

1
212
32123
4321234
543212345     ← rows = 5

Two inner loops per row build the symmetry. Follows the decreasing triangle in Program 36; next is Program 38.

How to Solve It

Start with fixed rows = 5, then generalize with scanf — optionally demo a smaller count for tracing.

MethodIdeaBest for
Desc then ascPrint i..2, then 1..iLearning, interviews, exams
User input rowsSame loops; read rows with scanfReusable demos and labs

Pseudocode

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

Cheat sheet

GoalPattern
Walk each rowfor (i = 1; i <= rows; i++)
Left half i..2for (j = i; j > 1; j--) printf("%d", j);
Right half 1..ifor (j = 1; j <= i; j++) printf("%d", j);
End the rowprintf("\n");
Digits on row i2 * i - 1

Printing Numbers vs Starting a New Line

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

Print all digits 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 value 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
212
32123
4321234
543212345

Worked Walkthrough — rows = 4

Trace each outer-loop value of i and list both halves of the palindrome.

iLeft i..2Right 1..iPrinted row
1(none)11
221, 2212
33, 21, 2, 332123
44, 3, 21, 2, 3, 44321234

Total digits for n rows = 1 + 3 + … + (2n - 1) = n² (here 16). That square sum is why time is O(n²).

C Programs

Three complete programs: fixed rows, scanf input, and a small demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded row count — descending half then ascending half on each row.

C
#include <stdio.h>

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

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Outer loop grows i. i runs from 1 to 5 — one longer palindrome each pass.

2. Left half. j runs from i down to 2 — empty when i = 1.

3. Right half + newline. j runs from 1 to i; then printf("\n") ends the row.

When i = 1 you get 1; when i = 3 you get 32123.

Example 2 — User Input Version

Read rows with scanf and reject invalid input. Same two-half core.

C
#include <stdio.h>

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

    printf("Enter rows: ");
    if (scanf("%d", &rows) != 1 || rows < 1)
        return 0;

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and validate. Exit early if scanf fails or rows < 1.

2. Same core. Only the hard-coded 5 becomes the user value.

3. Safer input tip. Prefer an explicit message instead of a silent exit:

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

Example 3 — Compact rows = 3

Same descending and ascending loops with a smaller row count for quick paper tracing.

C
#include <stdio.h>

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

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Same structure. Only rows changes from 5 to 3 — both inner loops stay identical.

2. Trace on paper. For i = 2: left prints 2, right prints 1 2 → 212.

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

Edge Cases & Pitfalls

Check these before calling the solution done.

one loop

Missing half

Skipping either inner loop breaks the palindrome. Keep both: i..2 then 1..i.

j >= 1

Doubled center

If the descending loop runs to 1, the digit 1 prints twice. Stop at j > 1.

\n inside

Broken rows

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

rows > 9

Multi-digit values

printf("%d", 10) prints two characters and the mirror drifts. Clamp demos to 1–9.

rows = 1

Smallest palindrome

Output is a single 1 — only the ascending loop runs. 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)
Small demo (Example 3)O(rows²)O(1)

Total digits = n² because odd numbers sum to a square. For rows = 5 that is 25 printed digits.

Key Takeaways

  • Rule: print i..2, then 1..i, on every row.
  • Center is 1: the ascending half always starts at 1, which sits in the middle of the palindrome.
  • Break the row: call printf("\n") only after both halves.
  • Complexity: O(n²) time from n² digits; O(1) extra space.

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

Frequently Asked Questions

Each row reads the same forward and backward. For example, 4321234 is symmetric around the center digit 1.
First, a loop prints i down to 2 (left half). Then another loop prints 1 up to i (right half). Together they create a mirrored sequence.
The first loop prints 4 3 2, and the second loop prints 1 2 3 4, which together form 4321234.
Row i prints 2i - 1 digits — one more than the previous row.
Program 36 is right-aligned with decreasing sequences. Program 37 builds a symmetric palindrome on each row with two inner loops.
Replace 5 with rows in the outer loop bound — see Example 2.
O(n²) for n rows because total digits printed are 1 + 3 + 5 + ... + (2n-1) = n².
Check scanf's return value and reject non-positive row counts so the outer loop has a valid range.

Did you know?

Each row is a palindrome: print i down to 2, then 1 up to i. Row i prints 2i - 1 digits — total digits across all rows = n².

Next: Continuous Decreasing-Row Triangle

Continue with a continuous counter that shortens each row using %3d formatting.

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