Star + Zero X-Pattern in C++

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

What You’ll Learn

How to print a pattern made of * and 0 where stars appear on:

  • The main diagonal (i == j)
  • The opposite diagonal (i == cols + 1 - j)
  • The center column (j == mid)

Everything else prints 0, creating an X shape with a vertical center line.

⭐ Pattern Output

For rows = 4 and cols = 9, the pattern looks like this:

Output
*000*000*
0*00*00*0
00*0*0*00
000***000
1

Complete C++ Program

We loop over a grid (4 rows × 9 columns) and decide whether to print * or 0 based on the position.

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

int main() {
    int i, j;

    for (i = 1; i <= 4; i++) {
        for (j = 1; j <= 9; j++) {
            if (i == j || j == 5 || i == 10 - j)
                cout << "*";
            else
                cout << "0";
        }
        cout << "\n";
    }

    return 0;
}

🧠 How It Works

1

Think of it as a grid

We visit every position (i, j) in a fixed-size grid.

Grid
2

Main diagonal

When i == j, we are on the main diagonal, so we print *.

Diagonal
3

Center column

When j == 5, we are in the middle column, so we print *.

Middle
4

Opposite diagonal

When i == 10 - j (since cols+1 = 10), we print * on the other diagonal.

Diagonal
=

X + center line

Every cell prints exactly one character, so runtime is proportional to the grid size.

2

Variation — User Input Version

Make the pattern scalable: for rows, use cols = 2*rows + 1 so there is always a center column.

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

int main() {
    int rows;
    cout << "Enter the number of rows: ";
    cin >> rows;

    if (rows <= 0) return 0;

    int cols = 2 * rows + 1;
    int mid = rows + 1;

    for (int i = 1; i <= rows; i++) {
        for (int j = 1; j <= cols; j++) {
            if (i == j || j == mid || i == (cols + 1 - j))
                cout << "*";
            else
                cout << "0";
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Use a different fill character (e.g., .) instead of 0
  • Print the full X by also printing the bottom half (mirror rows) if you want a square
  • Change * to digits like 1 to create a numeric version
  • Use separate rows and cols for a rectangular version
  • Build each line in a string first if you want extra spacing

Avoid

  • Hard-coding magic numbers (like 9 or 5) if you want scalability
  • Using even column counts when you need a center line
  • Printing extra spaces unless you adjust the diagonals
  • Skipping input validation when taking rows from the user

Key Takeaways

1

Main diagonal: i == j.

2

Opposite diagonal: i == cols + 1 - j.

3

Center line: j == mid.

4

Overall runtime is about O(rows²) when cols = 2*rows+1.

❓ Frequently Asked Questions

On row 4 (with 9 columns), the two diagonals meet near the center, and the middle column is also a star, creating three consecutive * characters.
Yes. Replace cout << "0" with cout << " ". (The pattern will be less visible, but the star shape remains.)
Increase rows. In the scalable version, the width automatically becomes 2*rows+1 so the center line always exists.
O(rows²) because the program visits a grid of roughly rows × (2*rows+1) cells.

Explore More C++ Number Patterns!

Once you can detect diagonals and middle lines, you can create many grid-based designs.

All Number Patterns →
Did you know?

The opposite diagonal test i == cols + 1 - j is a standard trick in matrix problems (it mirrors positions across the center).

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