C Palindrome Number Pattern (Increasing-Decreasing)

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

What Is This Pattern?

An increasing-decreasing pyramid prints each row as a palindrome: count up from i to the peak 2i - 1, then back down to i. Row i always has 2i - 1 digits.

Remember
Rule: m = i each row
      print ascending i..(2i-1) with m++
      m = m - 2   ← skip the peak
      print descending with m--

1
232
34543
4567654
567898765     ← rows = 5

Follows the alternating triangle in Program 51; next is the mirror diagonal pattern in Program 53.

How to Solve It

Set m = i each row. Print the ascending half with m++, step back with m = m - 2, then print the descending half with m--.

MethodIdeaBest for
Two halves + step-backAscend with m++, m -= 2, descend with m--Learning, interviews, exams
Compact traceDry-run with rows = 3 before coding rows = 5Paper tracing and labs

Pseudocode

Pseudocode
for i from 1 to rows:
    m = i
    for j from 1 to i:
        print m, then m = m + 1
    m = m - 2
    for k from 1 to (i - 1):
        print m, then m = m - 1
    print newline

Cheat sheet

GoalPattern
Pick each rowfor (i = 1; i <= rows; i++)
Start valuem = i;
Ascending halffor (j = 1; j <= i; j++) printf("%d", m++);
Skip the peakm = m - 2;
Descending halffor (k = 1; k < i; k++) printf("%d", m--);
Digits on row i2 * i - 1

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", m++)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 pyramid updates instantly — capped at 5 so every digit stays a single character (peak ≤ 9).

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

Live result rows = 5 · 25 digits
1
232
34543
4567654
567898765

Worked Walkthrough — Row i = 3

Trace both halves for row 3 — ascending 345, step back, then descending 43.

StepStatePrints
Startm = 3—
Ascend (3 times)m++ prints 3, 4, 5345
After ascendm = 6, then m = m - 2 → 4—
Descend (2 times)m-- prints 4, then 343

Full row: 34543. Digits on row i = 2i - 1; total = n² → O(n²).

C Programs

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

Example 1 — Fixed rows = 5

Hard-coded height — ascend with m++, step back with m = m - 2, descend with m--.

C
#include <stdio.h>

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

    for (i = 1; i <= rows; i++)
    {
        m = i;

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

        m = m - 2;

        for (k = 1; k < i; k++)
            printf("%d", m--);

        printf("\n");
    }

    return 0;
}

How It Works

1. Start each row. Set m = i so row 3 begins at 3, row 5 at 5.

2. Ascend to the peak. Print i digits with m++ — up to 2i - 1.

3. Step back, then descend. m = m - 2 skips the peak; the second loop prints i - 1 digits going down.

Example 2 — User Input Rows

Read rows with scanf and reject non-positive values.

C
#include <stdio.h>

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

    printf("Enter the number of rows: ");
    if (scanf("%d", &rows) != 1 || rows <= 0)
    {
        printf("Please enter a positive integer.\n");
        return 1;
    }

    for (i = 1; i <= rows; i++)
    {
        m = i;

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

        m = m - 2;

        for (k = 1; k < i; k++)
            printf("%d", m--);

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject bad input with a clear message.

2. Same core. Only the outer loop limit comes from the user — both halves stay identical.

3. Safer input tip. Cap demos so the peak stays a single digit (2 * rows - 1 ≤ 9):

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

Example 3 — Compact rows = 3

Same two-half structure with a smaller height for quick paper tracing.

C
#include <stdio.h>

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

    for (i = 1; i <= rows; i++)
    {
        m = i;

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

        m = m - 2;

        for (k = 1; k < i; k++)
            printf("%d", m--);

        printf("\n");
    }

    return 0;
}

How It Works

1. Three rows. Row 1 skips descending; row 2 prints 232; row 3 prints 34543.

2. Trace on paper. Confirm m = m - 2 after the peak so the middle digit is not doubled.

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.

m - 1

Doubled peak

If you use m = m - 1 (or skip the step-back), the peak digit prints twice. Keep m = m - 2.

k <= i

Extra descending digit

The descending loop must run i - 1 times: for (k = 1; k < i; k++). Using k <= i adds one too many.

\n inside

Broken rows

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

rows = 1

Single row

Output is just 1 — the descending loop never runs. A good sanity check.

rows > 5

Multi-digit values

Past 5, the peak exceeds 9 and values like 10 break the tight look. Cap demos at 5 or use spaced / fixed-width format.

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(n²)O(1)
Compact rows = 3 (Example 3)O(n²)O(1)

Row i prints 2i - 1 digits. Total = 1 + 3 + 5 + … + (2n - 1) = n². For n = 5 that is 25 digits.

Key Takeaways

  • Palindrome row: ascend i..(2i-1), then descend back to i.
  • Skip the peak: after ascending, m = m - 2 so the middle digit is not printed twice.
  • Break the row: call printf("\n") only after both halves.
  • Complexity: O(n²) time from n² digits; O(1) extra space.

One line: m = i; print ascending with m++, m -= 2, print descending with m--, then printf("\n").

Frequently Asked Questions

Row 3 starts at 3, prints up to 5 (345), then prints back down to 3 (43) after m = m - 2 — producing 34543.
Each row counts up from i to the peak 2i-1, then counts back down to i. The sequence reads the same left-to-right on each line.
After the increasing loop, m is one past the peak. Subtracting 2 moves it to the value just before the peak so the decreasing loop does not repeat the peak digit.
Step back with m = m - 2 before the decreasing loop. The decreasing loop then runs i-1 times, skipping the peak.
Change rows or read it from user input with scanf — see Example 2.
O(n²) for n rows because row i prints 2i-1 digits and 1+3+5+...+(2n-1) = n² total prints.
Program 51 uses a continuous counter with alternating direction across rows. Program 52 resets m = i each row and builds a palindromic line per row.
One row prints 1 — the decreasing loop k < i never runs when i = 1.

Did you know?

Each row is palindromic: print i..(2i-1) ascending, then back down with m = m - 2 to skip the peak. Row 3 prints 34543 — total digits = 1+3+5+…+(2n-1) = n² for n rows.

Next: Mirror Diagonal Number Pattern

Continue with a V-shaped pattern that prints 1..5 along both diagonals.

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