C Palindrome Number Triangle Pattern (Outer Peak)

Definition
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.
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.
Approach
How to Solve It
Start with fixed rows = 5, then generalize with scanf — optionally demo a smaller count for tracing.
| Method | Idea | Best for |
|---|
| Desc then asc | Print i..2, then 1..i | Learning, interviews, exams |
| User input rows | Same loops; read rows with scanf | Reusable demos and labs |
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
| Goal | Pattern |
|---|
| Walk each row | for (i = 1; i <= rows; i++) |
Left half i..2 | for (j = i; j > 1; j--) printf("%d", j); |
Right half 1..i | for (j = 1; j <= i; j++) printf("%d", j); |
| End the row | printf("\n"); |
Digits on row i | 2 * i - 1 |
Printing Numbers vs Starting a New Line
| API | Effect | Use for |
|---|
printf("%d", j) | Stays on the same line | Each digit in both halves |
printf("\n") | Ends the current line | After both inner loops |
Print all digits without a newline, then end the row once.
Try it
Live Preview
Change the row count and the palindrome triangle updates instantly — capped at 9 so every value stays a single character.
Trace
Worked Walkthrough — rows = 4
Trace each outer-loop value of i and list both halves of the palindrome.
i | Left i..2 | Right 1..i | Printed row |
|---|
1 | (none) | 1 | 1 |
2 | 2 | 1, 2 | 212 |
3 | 3, 2 | 1, 2, 3 | 32123 |
4 | 4, 3, 2 | 1, 2, 3, 4 | 4321234 |
Total digits for n rows = 1 + 3 + … + (2n - 1) = n² (here 16). That square sum is why time is O(n²).
Code
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.
#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;
}
1
212
32123
4321234
543212345
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.
#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:
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.
#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 loopMissing half
Skipping either inner loop breaks the palindrome. Keep both: i..2 then 1..i.
j >= 1Doubled center
If the descending loop runs to 1, the digit 1 prints twice. Stop at j > 1.
\n insideBroken rows
If printf("\n") sits inside either inner loop, each digit lands on its own line. Call the newline only after both halves.
rows > 9Multi-digit values
printf("%d", 10) prints two characters and the mirror drifts. Clamp demos to 1–9.
rows = 1Smallest palindrome
Output is a single 1 — only the ascending loop runs. A good sanity check.
scanfCheck the return value
If scanf fails, rows may be uninitialized — always test scanf(...) == 1.
Analysis
Time and Space Complexity
| Program | Time | Extra 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.
Remember
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
Developer, cloud engineer, and technical writer
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