Number & Star Diamond Pattern in C++

Beginner
⏱️ 6 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

How to print a number-and-star diamond pattern in C++. Each row repeats the row number, separated by *, then the pattern mirrors back down.

This pattern is a great way to practice nested loops plus a simple parity condition (j % 2).

⭐ Pattern Output

For rows = 5, the pattern looks like this:

Output
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
1

Complete C++ Program

Print rows from 1..rows, then mirror rows-1..1. Inside each row, print i on odd positions and * on even positions.

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

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

    for (i = 1; i <= rows; i++) {
        for (j = 1; j < i * 2; j++) {
            if (j % 2 == 0)
                cout << "*";
            else
                cout << i;
        }
        cout << "\n";
    }

    for (i = rows - 1; i >= 1; i--) {
        for (j = 1; j < i * 2; j++) {
            if (j % 2 == 0)
                cout << "*";
            else
                cout << i;
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Pick the size

int rows = 5; controls the maximum digit and the diamond height.

Setup
2

Top half: i from 1 to rows

The first outer loop prints increasing rows: 1, then 2*2, then 3*3*3, and so on.

Top half
3

Row width is 2i-1

The inner loop runs while j < 2i. That means it executes 2i-1 times.

Width
4

Odd/even positions decide what to print

If j is even, print *. If it’s odd, print the digit i.

Condition
=

Mirrored diamond

Mirroring back down (rows-1..1) completes the diamond-like pattern. Overall work grows roughly with \(n^2\).

2

Variation — User Input Version

Let the user decide the maximum row value using cin:

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

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

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

    for (i = 1; i <= rows; i++) {
        for (j = 1; j < i * 2; j++) {
            if (j % 2 == 0)
                cout << "*";
            else
                cout << i;
        }
        cout << "\n";
    }

    for (i = rows - 1; i >= 1; i--) {
        for (j = 1; j < i * 2; j++) {
            if (j % 2 == 0)
                cout << "*";
            else
                cout << i;
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Replace * with another separator like - or |
  • Right-align the diamond by printing leading spaces before each row
  • Build a full centered diamond by adding spaces and using a fixed width
  • Print the row number sequence (1..i..1) instead of repeating a single digit
  • Validate input (e.g., ensure rows > 0)

Avoid

  • Using endl inside loops unnecessarily (it flushes output)
  • Allowing negative rows without guarding input
  • Printing * at the end of each row (this pattern avoids a trailing separator)
  • Hard-coding 5 everywhere instead of using rows

Key Takeaways

1

Row i has 2i-1 characters: digits on odd positions and stars on even positions.

2

Using two outer loops (up then down) creates a mirrored diamond-like shape.

3

The condition j % 2 is a simple way to alternate between digit and separator.

4

This technique generalizes to other alternating patterns (numbers/spaces, stars/dashes, etc.).

❓ Frequently Asked Questions

Because that makes the loop run 2*i - 1 times, which is exactly the length needed to print i digits with i-1 separators in between.
Skip the even-position printing branch and print only i. Or use a loop that prints i exactly i times.
Yes. Print leading spaces before each row (based on rows - i) so the widest row aligns to the center.
O(n²) for n rows because total printed characters sum up roughly like 1+3+5+…+(2n-1) plus the mirrored part.

Explore More C++ Number Patterns!

Next, try centering this pattern or replacing the separator to create a fresh look.

All Number Patterns →
Did you know?

Alternating output using j % 2 is a common trick for patterns. The same idea can alternate digits, stars, spaces, or even different colors in terminal-based UIs.

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