C Palindromic Number Pyramid Pattern

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

What Is This Pattern?

A palindromic number pyramid is a centered triangle where row i prints 1..i ascending, then i-1..1 descending — so every row reads the same forward and backward.

Remember
Rule: spaces (rows−i)×2, then 1..i, then i−1..1

        1
      1 2 1
    1 2 3 2 1
  1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1     ← rows = 5

Follows the column-wise triangle in Program 55; next is the diagonal mirror pyramid in Program 57.

How to Solve It

Per row: center with leading spaces, climb to the peak, then mirror down without repeating the peak.

MethodIdeaBest for
Three inner loopsSpaces, ascending 1..i, descending i-1..1Learning, interviews, exams
Skip peak in descentStart descending at i-1Clean palindrome (no double peak)

Pseudocode

Pseudocode
for i from 1 to rows:
    print (rows - i) pairs of spaces
    for k from 1 to i:
        print k and a space
    for k from i - 1 down to 1:
        print k and a space
    print newline

Cheat sheet

GoalPattern
Outer loopfor (i = 1; i <= rows; i++)
Leading spacesfor (s = 1; s <= rows - i; s++) printf(" ");
Ascending halffor (k = 1; k <= i; k++) printf("%d ", k);
Descending halffor (k = i - 1; k >= 1; k--) printf("%d ", k);
Digits on row i2 * i - 1

Printing Numbers vs Starting a New Line

APIEffectUse for
printf(" ") / printf("%d ", k)Stays on the same lineSpaces and each digit
printf("\n")Ends the current lineAfter spaces + ascending + descending

Build the whole row without a newline, then end the row once.

Live Preview

Change the row count and the centered palindromic pyramid updates instantly — capped at 9 for readable single-digit demos.

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

Live result rows = 5 · bottom peak 5
        1 
      1 2 1 
    1 2 3 2 1 
  1 2 3 4 3 2 1 
1 2 3 4 5 4 3 2 1 

Worked Walkthrough — Row i = 3 when rows = 5

Trace one middle row so centering and the skip-peak descent are clear.

StepLoopPrints
1s = 1..2 (rows - i) (4 spaces)
2k = 1..31 2 3
3k = 2..12 1

Full row: 1 2 3 2 1. Peak 3 appears once because descent starts at i - 1.

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 — spaces, ascending, then descending on each row.

C
#include <stdio.h>

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

    for (i = 1; i <= rows; i++)
    {
        for (s = 1; s <= (rows - i); s++)
            printf("  ");

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Center. Print (rows - i) pairs of spaces so upper rows sit further right.

2. Ascend. printf("%d ", k) from 1 to i reaches the peak.

3. Descend. Start at i - 1 so the peak is not printed twice, then printf("\n").

Example 2 — User Input Rows

Read rows with scanf, validate, then run the same three-loop core.

C
#include <stdio.h>

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

    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++)
    {
        for (s = 1; s <= (rows - i); s++)
            printf("  ");

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject bad input before printing.

2. Same core. Spacing and palindrome loops match Example 1 — only rows comes from the user.

3. Safer input tip. Cap demos for readable console output:

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 — Compact rows = 3

Same structure with only three rows — easy to trace on paper.

C
#include <stdio.h>

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

    for (i = 1; i <= rows; i++)
    {
        for (s = 1; s <= (rows - i); s++)
            printf("  ");

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Three rows. Row 1: four leading spaces + 1. Row 2: two spaces + 1 2 1.

2. Trace on paper. Confirm descent starts at i - 1 so row 3 is 1 2 3 2 1, not 1 2 3 3 2 1.

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.

k = i..1

Double peak

If descent starts at i, row 3 becomes 1 2 3 3 2 1. Always start at i - 1.

one space

Weak centering

Use printf(" ") (two spaces) per indent so digits align under each other with %d.

\n inside

Broken rows

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

rows = 1

Single digit

Output is just 1 with no leading spaces — a good sanity check for input validation.

rows > 9

Multi-digit width

Digits 10+ break neat alignment with %d. Cap demos at 9 or use a fixed field width.

scanf

Check the return value

Validate scanf before looping — a failed read leaves rows uninitialized.

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 O(i) spaces and digits; summing over n rows is O(n²). Only a few loop variables are needed.

Key Takeaways

  • Three parts per row: leading spaces, ascending 1..i, descending i-1..1.
  • Skip the peak: descent starts at i - 1 so the middle digit appears once.
  • Center with pairs: (rows - i) times printf(" ") keeps digits aligned.
  • Complexity: O(n²) time from ~2i-1 digits per row; O(1) extra space.

One line: spaces (rows-i), then 1..i, then i-1..1, then newline.

Frequently Asked Questions

Each row reads the same forward and backward: 1, 1 2 1, 1 2 3 2 1, and so on.
Print (rows - i) pairs of spaces before the numbers on row i — more spaces on upper rows, fewer on lower rows.
A centered pyramid where row i shows 1..i ascending then i-1..1 descending, e.g. row 3: ' 1 2 3 2 1 '.
Program 55 fills a 2D array column-wise. Program 56 prints palindromic digits directly with spaces, ascending, and descending loops.
Starting at i-1 avoids printing the peak digit twice — row i already printed i in the ascending loop.
Change rows or read it from user input with scanf — see Example 2.
O(n²) for n rows because each row prints O(n) spaces and digits.
Yes. Print this pyramid for the top half, then mirror rows from rows-1 down to 1 for the bottom half.

Did you know?

Each row prints 1..i..1 with leading spaces for centering. Row i has 2i-1 digits — total work grows as O(n²) for n rows.

Next: Diagonal Mirror Number Pyramid

Continue with a pyramid that prints the row number only on the left and right diagonals.

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