C++ Diamond Diagonal Number Pattern

Beginner
6 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 (i = 1..rows), then mirror it downward (i = rows-1..1) so digits climb to the tip and descend again — a full diamond of 2n - 1 lines.

Remember
Rule: Program 53 row logic, twice — top then bottom (skip peak)

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

Unlike Program 53 (top V only), this shape adds a second outer loop so the pattern closes into a diamond.

How to Solve It

Reuse the same row builder twice: top i = 1..rows, bottom i = rows-1..1.

MethodIdeaBest for
Two outer loopsTop V-half, then mirror without repeating the tipLearning, interviews, demos
Compact traceSame logic with rows = 3Quick dry-runs on paper

Pseudocode

Pseudocode
for i from 1 to rows:            // top half
    print left (i == j) and right (i == k) halves
    print newline
for i from (rows - 1) down to 1: // bottom half
    print left (i == j) and right (i == k) halves
    print newline

// left:  j from 1 to rows
// right: k from (rows - 1) down to 1

Cheat sheet

GoalPattern
Top halffor (i = 1; i <= rows; i++)
Bottom halffor (i = rows - 1; i >= 1; i--)
Left diagonal(i == j ? cout << j : cout << " ")
Right diagonal(i == k ? cout << k : cout << " ")
Skip tip twiceBottom starts at rows - 1
End the rowcout << "\n";

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << j / cout << " "Stays on the same lineEach cell (digit or space)
cout << "\n"Ends the current lineAfter both inner loops

Print cells without a newline, then end the row once. Spaces keep both diagonals aligned on every line of the diamond.

Live Preview

Change the peak row count and the full diamond updates instantly.

Use peak rows from 2 to 9. Total lines = 2 × peak - 1. Tap a chip or type a value — the preview redraws as you go.

Live result peak 5 · 9 lines
1       1
 2     2 
  3   3  
   4 4   
    5    
   4 4   
  3   3  
 2     2 
1       1

Worked Walkthrough

How the two outer loops build a diamond for rows = 5.

Passi valuesWhat appears
Top1, 2, 3, 4, 5V opens → tip at 5
Bottom4, 3, 2, 1mirror down (skips tip)
Sample linei = 33 3

Starting the bottom loop at rows - 1 avoids printing the tip twice. Each line still uses the same left/right diagonal checks as Program 53.

C++ Programs

Three complete programs: fixed rows = 5, cin input, and a compact rows = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded peak — top V-half, then bottom mirror for a complete 9-line diamond.

C++
#include <iostream>
using namespace std;

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

    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= rows; j++)
            (i == j ? cout << j : cout << " ");

        for (k = rows - 1; k >= 1; k--)
            (i == k ? cout << k : cout << " ");

        cout << "\n";
    }

    for (i = rows - 1; i >= 1; i--)
    {
        for (j = 1; j <= rows; j++)
            (i == j ? cout << j : cout << " ");

        for (k = rows - 1; k >= 1; k--)
            (i == k ? cout << k : cout << " ");

        cout << "\n";
    }

    return 0;
}

How It Works

1. Top half opens the V. i runs from 1 to 5 — same row builder as Program 53.

2. Bottom half mirrors out. i runs from 4 down to 1 so the tip is not printed twice.

3. Same cell rule everywhere. Left i == j, right i == k, spaces elsewhere, then "\n".

Example 2 — User Input Rows

Read the peak count at runtime — both halves scale automatically.

C++
#include <iostream>
using namespace std;

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

    cout << "Enter the number of rows: ";
    cin >> rows;

    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= rows; j++)
            (i == j ? cout << j : cout << " ");

        for (k = rows - 1; k >= 1; k--)
            (i == k ? cout << k : cout << " ");

        cout << "\n";
    }

    for (i = rows - 1; i >= 1; i--)
    {
        for (j = 1; j <= rows; j++)
            (i == j ? cout << j : cout << " ");

        for (k = rows - 1; k >= 1; k--)
            (i == k ? cout << k : cout << " ");

        cout << "\n";
    }

    return 0;
}

How It Works

1. Same two-pass structure. Only the source of rows changes from a literal to cin.

2. Size follows rows. Entering 4 yields 7 lines (2 × 4 - 1).

3. Validate in real apps. Prefer checking cin failure and requiring rows >= 1 (tip below).

Safer input tip
if (!(cin >> rows) || rows < 1)
{
    cout << "Please enter a positive integer.\n";
    return 1;
}

Example 3 — Compact rows = 3

Smaller peak for quick tracing — five lines total.

C++
#include <iostream>
using namespace std;

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

    for (i = 1; i <= rows; i++)
    {
        for (j = 1; j <= rows; j++)
            (i == j ? cout << j : cout << " ");

        for (k = rows - 1; k >= 1; k--)
            (i == k ? cout << k : cout << " ");

        cout << "\n";
    }

    for (i = rows - 1; i >= 1; i--)
    {
        for (j = 1; j <= rows; j++)
            (i == j ? cout << j : cout << " ");

        for (k = rows - 1; k >= 1; k--)
            (i == k ? cout << k : cout << " ");

        cout << "\n";
    }

    return 0;
}

How It Works

1. Five lines total. Top prints three rows; bottom adds two more (i = 2..1).

2. Same formula. Nothing changes except rows — proving the pattern scales.

3. Tip stays unique. The peak 3 appears once in the middle.

Edge Cases & Pitfalls

Check these before calling the solution done.

Bottom start

Start the bottom loop at rows - 1

Starting at rows duplicates the tip and breaks the diamond.

Missing bottom

Forgetting the second outer loop

That is Program 53 (V only). Add the bottom pass for the full diamond.

Right start

Right inner loop starts at rows - 1

Same as Program 53 — skips the center column so the tip is a single digit.

Bad cin

Validate input

Check cin >> rows and require rows >= 1 before the loops.

Time and Space Complexity

ProgramTimeExtra space
Full diamond (Examples 1–3)O(n²)O(1)

There are 2n - 1 lines and each scans about 2n - 1 characters, so work is quadratic in n. Only a few integers of extra memory.

Key Takeaways

  • Rule: print digit when i == j or i == k; else space — same as Program 53.
  • Two passes: top i = 1..rows, bottom i = rows-1..1.
  • Break the row: call cout << "\n" only after both inner loops.
  • Complexity: O(n²) time; O(1) extra space.

One line: print Program 53’s V for i = 1..rows, then repeat for i = rows-1..1 to finish the diamond.

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 peak row twice. rows-1 skips the tip already printed by the top half.
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.
Printing a digit or space stays on the same line. Printing a newline ends the current row. Cells use cout without a newline; the row break uses cout << "\n" after both inner loops.
O(n²) for n rows because the diamond has about 2n-1 lines and each line scans about 2n-1 positions.
Change rows or read it from user input with cin — see Example 2.
After cin >> rows, check failure and require rows >= 1 before the loops.

Did you know?

Program 53’s V 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 each — O(n²) overall.

Next: Column-Wise Number Triangle

Continue with the next pattern in the C++ number-pattern series.

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