Number & Star Diamond Pattern in C++

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:
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1Complete C++ Program
Print rows from 1..rows, then mirror rows-1..1. Inside each row, print i on odd positions and * on even positions.
#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
Pick the size
int rows = 5; controls the maximum digit and the diamond height.
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.
Row width is 2i-1
The inner loop runs while j < 2i. That means it executes 2i-1 times.
Odd/even positions decide what to print
If j is even, print *. If it’s odd, print the digit i.
Mirrored diamond
Mirroring back down (rows-1..1) completes the diamond-like pattern. Overall work grows roughly with \(n^2\).
Variation — User Input Version
Let the user decide the maximum row value using cin:
#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
endlinside 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
Row i has 2i-1 characters: digits on odd positions and stars on even positions.
Using two outer loops (up then down) creates a mirrored diamond-like shape.
The condition j % 2 is a simple way to alternate between digit and separator.
This technique generalizes to other alternating patterns (numbers/spaces, stars/dashes, etc.).
❓ Frequently Asked Questions
2*i - 1 times, which is exactly the length needed to print i digits with i-1 separators in between.i. Or use a loop that prints i exactly i times.rows - i) so the widest row aligns to the center.Explore More C++ Number Patterns!
Next, try centering this pattern or replacing the separator to create a fresh look.
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.
12 people found this page helpful
