Palindromic Pattern with Decreasing Spaces in C

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Spacing + Symmetry

What You’ll Learn

How to print a palindromic number pattern where the inner gap shrinks each row. Each row has three parts: ascending numbers, spaces, then descending numbers.

The last row has no gap, so it becomes a continuous palindrome: 1234554321.

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1        1
12      21
123    321
1234  4321
1234554321
1

Complete C Program

We print 1..i, then (rows - i) * 2 spaces, then i..1.

c
#include <stdio.h>

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

    for (i = 1; i <= 5; i++) {
        for (j = 1; j <= i; j++) {
            printf("%d", j);
        }
        for (k = 1; k <= (5 - i) * 2; k++) {
            printf(" ");
        }
        for (j = i; j >= 1; j--) {
            printf("%d", j);
        }
        printf("\n");
    }

    return 0;
}

🧠 How It Works

1

Outer loop controls rows

for (i = 1; i <= rows; i++) prints 5 rows.

Row control
2

Print ascending numbers

for (j = 1; j <= i; j++) prints 1..i.

Ascending
3

Print shrinking spaces

(rows - i) * 2 makes the gap shrink by 2 each row: 8, 6, 4, 2, 0.

Spacing
4

Print descending numbers

for (j = i; j >= 1; j--) prints i..1 to mirror the left side.

Descending
=

Palindromic symmetry

Left and right parts mirror each other with a decreasing inner gap.

2

Variation — User Input Version

Read rows from the user and compute the spaces using (rows - i) * 2.

c
#include <stdio.h>

int main() {
    int rows;
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    int i, j, k;
    for (i = 1; i <= rows; i++) {
        for (j = 1; j <= i; j++) {
            printf("%d", j);
        }
        for (k = 1; k <= (rows - i) * 2; k++) {
            printf(" ");
        }
        for (j = i; j >= 1; j--) {
            printf("%d", j);
        }
        printf("\n");
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Remove the spaces to make a continuous palindrome
  • Change the spacing formula for different gap styles
  • Use a custom gap character like - or .
  • Print spaces on both sides to center the whole shape
  • Use letters to create an alphabet palindrome

Avoid

  • Forgetting the newline after each row
  • Using a negative row count without validation
  • Printing tabs (they don’t align consistently)
  • Mixing ascending/descending loop bounds

Key Takeaways

1

Each row has three parts: numbers up, spaces, numbers down.

2

The gap is (rows - i) * 2, so it shrinks each row.

3

The result is palindromic because left and right parts mirror.

4

Nested loops + spacing control show up in many classic patterns.

❓ Frequently Asked Questions

Each row is symmetric: it prints 1..i, then spaces, then i..1. The spaces decrease until the last row has none.
It makes the gap shrink evenly as the sequences grow, keeping the output visually balanced (8, 6, 4, 2, 0 for 5 rows).
Yes. Remove the space loop and the pattern becomes continuous.
O(n²) for n rows because total printed characters scale quadratically.

Explore More C Number Patterns!

Keep going with more spacing and symmetry patterns to sharpen your logic.

All Number Patterns →
Did you know?

Spacing is one of the most common pattern-printing tricks. By changing only the gap loop, you can transform the same numbers into diamonds, hourglasses, and many other shapes.

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