C Hollow Number Diamond Pattern

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

What Is This Pattern?

A diagonal mirror number diamond is Program 57’s inverse-V pyramid plus a mirrored bottom half — digit i only on the left and right diagonals, spaces everywhere else.

Remember
Rule: top i = 1..rows, bottom i = rows−1..1
      same left/right diagonal print as Program 57

    1
   2 2
  3   3
 4     4
5       5
 4     4
  3   3
   2 2
    1     ← rows = 5 (9 lines)

Follows the diagonal mirror pyramid in Program 57; next is the hollow square border in Program 59.

How to Solve It

Reuse Program 57’s diagonal row logic twice: climb 1..rows, then descend rows-1..1 so the peak line appears once.

MethodIdeaBest for
Two outer loopsTop 1..rows, bottom rows-1..1Learning, interviews, exams
Shared row bodySame left/right diagonal loops in both halvesAvoiding duplicated logic bugs

Pseudocode

Pseudocode
function print_row(i, rows):
    for j from rows down to 1:
        if i == j: print i else print space
    for k from 2 to rows:
        if i == k: print i else print space
    print newline

for i from 1 to rows:
    print_row(i, rows)
for i from rows - 1 down to 1:
    print_row(i, rows)

Cheat sheet

GoalPattern
Top halffor (i = 1; i <= rows; i++)
Bottom halffor (i = rows - 1; i >= 1; i--)
Left diagonalfor (j = rows; j >= 1; j--) printf(i == j ? "%d" : " ", j);
Right diagonalfor (k = 2; k <= rows; k++) printf(i == k ? "%d" : " ", k);
Total lines2 * rows - 1

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", i) / printf(" ")Stays on the same lineEach diagonal digit or filler space
printf("\n")Ends the current lineAfter both left and right loops

Print digits and spaces without a newline, then end the row once.

Live Preview

Change the half-height and the hollow diamond 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 · 9 lines
    1    
   2 2   
  3   3  
 4     4 
5       5
 4     4 
  3   3  
   2 2   
    1    

Worked Walkthrough — rows = 5

See how the two outer loops share one row body and why the bottom starts at rows - 1.

Halfi valuesLines produced
Top1..51 → 2 2 → … → 5
Bottom4..14 → 3 3 → … → 1

Total lines = 2×5−1 = 9. Starting the bottom at 5 would print the peak twice.

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 half-height — top pyramid, then mirrored bottom with the same diagonal logic.

C
#include <stdio.h>

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

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

        for (k = 2; k <= rows; k++)
            printf(i == k ? "%d" : " ", k);

        printf("\n");
    }

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

        for (k = 2; k <= rows; k++)
            printf(i == k ? "%d" : " ", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Top half. Same as Program 57: left j = rows..1, right k = 2..rows.

2. Bottom half. Replay the same row body for i = rows - 1 down to 1.

3. One peak. Skipping i = rows on the way down keeps the middle line unique.

Example 2 — User Input Rows

Read half-height with scanf, validate, then run both outer loops.

C
#include <stdio.h>

int main(void)
{
    int rows;
    int i, j, 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 (j = rows; j >= 1; j--)
            printf(i == j ? "%d" : " ", j);

        for (k = 2; k <= rows; k++)
            printf(i == k ? "%d" : " ", k);

        printf("\n");
    }

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

        for (k = 2; k <= rows; k++)
            printf(i == k ? "%d" : " ", k);

        printf("\n");
    }

    return 0;
}

How It Works

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

2. Same core. Top and bottom 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

Five-line diamond — easy to confirm the bottom starts at 2, not 3.

C
#include <stdio.h>

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

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

        for (k = 2; k <= rows; k++)
            printf(i == k ? "%d" : " ", k);

        printf("\n");
    }

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

        for (k = 2; k <= rows; k++)
            printf(i == k ? "%d" : " ", k);

        printf("\n");
    }

    return 0;
}

How It Works

1. Five lines. Top prints 1, 2 2, 3 3; bottom reprints 2 2 and 1.

2. Trace on paper. Confirm the peak 3 3 appears once — bottom starts at rows - 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.

i = rows..1

Double peak

If the bottom loop starts at rows, the widest line prints twice. Always start at rows - 1.

k = 1

Triple digit on a row

If the right loop starts at 1, the center column can print a third digit. Keep k = 2.

\n inside

Broken rows

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

rows = 1

Single line

Output is just 1 — the bottom loop (rows-1..1) does not run.

rows > 9

Multi-digit width

Digits 10+ break neat column 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)

You print 2n−1 lines, each scanning about 2n−1 positions → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Pyramid + mirror: top 1..rows, bottom rows-1..1, same diagonal row body.
  • Skip the peak twice: bottom starts at rows - 1.
  • Two diagonals per row: print i when i == j or i == k; spaces elsewhere.
  • Complexity: O(n²) time from 2n−1 lines × ~2n positions; O(1) extra space.

One line: Program 57 once upward, then again from rows-1 down to 1.

Frequently Asked Questions

Each row prints the same digit on the left diagonal and the right diagonal; spaces fill the remaining positions.
The top half already printed the peak row — starting at rows-1 avoids duplicating the middle line.
An inverse-V pyramid from 1 to rows, then mirrored back down to 1 — 2*rows-1 lines total.
Program 57 prints only the top pyramid half. Program 58 adds a second outer loop from rows-1 down to 1 for the bottom half.
Each column prints either the row digit or a space — those conditions pick exactly the two diagonal positions.
Change rows or read it from user input with scanf — see Example 2.
O(n²) for n rows because you print 2n-1 lines, each scanning about 2n positions.
Yes. Replace the digit printf with printf("*") in both diagonal print branches.

Did you know?

Print the Program 57 pyramid for the top half, then mirror with for (i = rows-1; i >= 1; i--). Total lines = 2×rows-1 — each row scans about 2×rows-1 positions.

Next: Hollow Square Border Numbers

Continue with consecutive numbers around a hollow square border.

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