C Diamond Diagonal Number Pattern

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

What Is This Pattern?

A mirror diagonal diamond extends Program 53’s V-shape: print the top half from 1 to rows, then mirror the same row logic from rows - 1 back to 1 — total 2n - 1 lines.

Remember
Rule: top    i = 1..rows
      bottom i = rows-1..1
      each row: left i == j, right i == k

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

Follows the mirror diagonal V in Program 53; next is the column-wise number triangle in Program 55.

How to Solve It

Reuse Program 53’s row logic twice — once counting up, once counting down from rows - 1 so the peak row is not duplicated.

MethodIdeaBest for
Two outer loopsTop 1..rows, bottom rows-1..1; same inner diagonalsLearning, interviews, exams
Compact traceDry-run with rows = 3 (5 lines) before coding rows = 5Paper tracing and labs

Pseudocode

Pseudocode
for i from 1 to rows:                 // top half
    for j from 1 to rows:
        print i if i == j else a space
    for k from (rows - 1) down to 1:
        print i if i == k else a space
    print newline

for i from (rows - 1) down to 1:      // bottom half
    // same row logic as above
    print newline

Cheat sheet

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

Printing Numbers vs Starting a New Line

APIEffectUse for
printf("%d", j) / printf(" ")Stays on the same lineEach column in both halves
printf("\n")Ends the current lineAfter both inner loops

Print all columns without a newline, then end the row once.

Live Preview

Change the peak row count and the full diamond updates instantly — capped at 9 so every digit stays a single character.

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

Live result rows = 5 · 9×9 cells
1       1
 2     2 
  3   3  
   4 4   
    5    
   4 4   
  3   3  
 2     2 
1       1

Worked Walkthrough — Peak rows = 5

Count how the two outer loops build nine lines without repeating the tip.

Outer loopi valuesLines printed
Top1, 2, 3, 4, 5V-half ending with tip 5
Bottom4, 3, 2, 1Mirror back — skips i = 5

Total lines = 5 + 4 = 9 = 2n - 1. Each line has 2n - 1 characters → O(n²).

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 peak — top loop 1..rows, bottom loop rows-1..1, same inner diagonal logic each row.

C
#include <stdio.h>

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

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

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

        printf("\n");
    }

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Top half. Same as Program 53 — i runs 1 to 5, printing the growing V.

2. Bottom half. i runs 4 down to 1 with identical inner loops — mirrors without repeating the tip.

3. Result. Nine lines forming a full diamond with digits on both diagonals.

Example 2 — User Input Rows

Read peak rows with scanf and reject non-positive values.

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

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

        printf("\n");
    }

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Prompt and validate. Reject bad input with a clear message.

2. Same core. Both outer loops and both diagonal conditions adjust automatically from rows.

3. Safer input tip. Cap demos to single-digit peaks:

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 two-outer-loop structure with a smaller peak for quick paper tracing.

C
#include <stdio.h>

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

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

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

        printf("\n");
    }

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

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

        printf("\n");
    }

    return 0;
}

How It Works

1. Five lines. Top prints i = 1..3; bottom prints i = 2..1 — five lines total.

2. Trace on paper. Confirm the tip 3 appears once, then the V opens again downward.

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 twice

Doubled tip

If the bottom loop starts at i = rows, the middle row prints twice. Keep for (i = rows - 1; i >= 1; i--).

missing bottom

V instead of diamond

Omitting the second outer loop leaves only Program 53’s V-half. Add the rows-1..1 pass after the tip.

k = rows

Doubled center column

If the right inner loop starts at k = rows, the middle digit doubles on each row. Keep k = rows - 1.

\n inside

Broken rows

If printf("\n") sits inside either half, each column lands on its own line. Call the newline only after both inner loops.

rows = 1

Single tip

Output is just 1 — the bottom loop does not run. A good sanity check.

scanf

Check the return value

If scanf fails, rows may be uninitialized — always test scanf(...) == 1.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact rows = 3 (Example 3)O(n²)O(1)

2n - 1 lines × 2n - 1 characters each → about 4n² prints. For n = 5 that is 81 characters across 9 lines.

Key Takeaways

  • Two passes: top i = 1..rows, bottom i = rows-1..1 — same i == j / i == k row logic.
  • Skip the tip twice: bottom starts at rows - 1; right half starts at rows - 1.
  • Break the row: call printf("\n") only after both inner loops.
  • Complexity: O(n²) time from (2n-1)² characters; O(1) extra space.

One line: top 1..rows, bottom rows-1..1, each row prints diagonal digits with spaces, then printf("\n").

Frequently Asked Questions

The first loop prints the top V-half from i = 1 to rows. The second prints the bottom half from rows-1 down to 1, mirroring the shape.
Starting at rows would print the middle row twice. rows-1 skips the peak row already printed by the top half.
It prints digits on both diagonals per row — top half grows to rows, then the bottom half mirrors back to 1, forming a symmetric diamond.
Program 53 prints only the top V-half. Program 54 adds a second outer loop to mirror the same row logic downward.
The left loop uses i == j for the main diagonal. The right loop uses i == k for the mirrored diagonal, with spaces elsewhere.
Change rows or read it from user input with scanf — see Example 2.
O(n²) for n rows because the diamond has about 2n-1 lines and each line scans about 2n-1 positions.
One line prints a single 1 — the bottom loop starts at 0 and does not run.

Did you know?

Program 53’s V-shape becomes a full diamond by adding a second outer loop from rows-1 down to 1. Total lines = 2n-1 with about 2n-1 characters per line — O(n²) overall.

Next: Column-Wise Number Triangle

Continue with a triangle filled column-by-column into a 2D array, then printed row-wise.

Program 55 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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