C Hollow Number Pyramid Pattern

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

What Is This Pattern?

A diagonal mirror number pyramid prints the row digit only on the left and right diagonals — spaces fill the rest — forming an inverse-V (hollow) pyramid.

Remember
Rule: left j = rows..1 (print i if i==j), right k = 2..rows (print i if i==k)

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

Follows the palindromic pyramid in Program 56; next is the full diagonal mirror diamond in Program 58.

How to Solve It

Scan left columns downward from rows, then right columns from 2 — print the row digit only when the column index matches i.

MethodIdeaBest for
Two conditional loopsLeft j = rows..1, right k = 2..rowsLearning, interviews, exams
Start right at 2Skips the shared center columnExactly two digits per row (when i > 1)

Pseudocode

Pseudocode
for i from 1 to 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

Cheat sheet

GoalPattern
Outer loopfor (i = 1; i <= rows; 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);
Width per row2 * rows - 1 character positions
Digits per row1 when i == 1; otherwise 2

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 row count and the inverse-V 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 · width 9
    1    
   2 2   
  3   3  
 4     4 
5       5

Worked Walkthrough — Row i = 3 when rows = 5

Trace one middle row so both diagonal hits and the skip-center right loop are clear.

LoopValuesPrints
Left j5..13 (digit at j = 3)
Right k2..53 (digit at k = 3)

Full row: 3 3. Starting the right loop at 2 avoids printing a third 3 at the center.

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 — left diagonal down from rows, right diagonal from 2.

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");
    }

    return 0;
}

How It Works

1. Left half. j counts down from rows; print i when i == j, else a space.

2. Right half. k starts at 2 so the center column is not printed twice.

3. Newline. Call printf("\n") only after both loops finish the row.

Example 2 — User Input Rows

Read rows with scanf, validate, then run the same diagonal logic.

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");
    }

    return 0;
}

How It Works

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

2. Same core. Left and right diagonal 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 every i == j / i == k check.

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");
    }

    return 0;
}

How It Works

1. Five positions. Each row spans 2×3−1 = 5 character slots.

2. Trace on paper. Row 2 hits at left j = 2 and right k = 2 → 2 2.

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 = 1

Triple digit on a row

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

j = 1..rows

Mirrored the wrong way

The left loop must count j down from rows — ascending 1..rows flips the left diagonal.

\n inside

Broken rows

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

rows = 1

Single digit

Output is just 1 — the right loop (k = 2..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)

Each of n rows scans about 2n−1 positions → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Two diagonals: print digit i when i == j (left) or i == k (right).
  • Skip the center: right loop starts at k = 2 so the middle column is not duplicated.
  • Left counts down: j = rows..1 places the left diagonal correctly.
  • Complexity: O(n²) time from ~2n positions per row; O(1) extra space.

One line: left j = rows..1 and right k = 2..rows, print i only on the diagonals.

Frequently Asked Questions

The left loop places the row number on the left diagonal; the right loop mirrors it on the right diagonal.
Starting at k = 2 avoids duplicating the center column — each row prints the digit at most twice.
Row i shows digit i on two diagonals with spaces between, e.g. for rows=5, row 3 is ' 3 3 '.
Program 56 prints a full palindromic row (1..i..1). Program 57 prints only the row number twice on mirror diagonals.
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 each row scans about 2n character positions.
Yes. Replace the digit printf with printf("*") in both diagonal print branches.

Did you know?

Each row prints the row number twice — once on the left diagonal and once on the right — with spaces everywhere else. Total positions per row = 2×rows-1.

Next: Diagonal Mirror Number Diamond

Continue by mirroring this pyramid downward into a full hollow diamond.

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