Descending Numbers with Diagonal Star in C++

What You’ll Learn
How to print descending numbers from 5 to 1 on each row in C++, while replacing one position with a * on the diagonal.
The key trick is a simple condition inside the inner loop: print * when i == j, otherwise print the current number j.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
5432*
543*1
54*21
5*321
*4321Complete C++ Program
The inner loop prints rows..1 each row, and the diagonal value is replaced with *.
#include <iostream>
using namespace std;
int main() {
int rows = 5;
int i, j;
for (i = 1; i <= rows; i++) {
for (j = rows; j >= 1; j--) {
if (i == j)
cout << "*";
else
cout << j;
}
cout << "\n";
}
return 0;
}🧠 How It Works
Set the size
int rows = 5; defines both the number of rows and the range of numbers printed.
Outer loop (row index)
for (i = 1; i <= rows; i++) selects which row you’re printing.
Inner loop (descending print)
for (j = rows; j >= 1; j--) prints the sequence rows..1 on each line.
Replace the diagonal with *
When i == j, print * instead of the number. This creates the diagonal star from the top-right to the bottom-left.
Diagonal star pattern
Each row prints rows characters, so total work is O(n²) for n rows.
Variation — User Input Version
Let the user decide the number of rows 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 = rows; j >= 1; j--) {
if (i == j)
cout << "*";
else
cout << j;
}
cout << "\n";
}
return 0;
}💡 Tips for Enhancement
Try These
- Validate input (e.g., check
cin.fail()) before usingrows - Replace the diagonal with a different symbol like
#or@ - Flip the diagonal by changing the condition to
i + j == rows + 1 - Add spaces between prints for a grid look:
cout << j << ' ' - Combine with other patterns (e.g., stars on both diagonals)
Avoid
- Mixing up loop directions (this pattern relies on
jcounting down) - Forgetting the newline after each row
- Hard-coding
5in multiple places instead of usingrows - Using
endlinside loops unnecessarily
Key Takeaways
The inner loop prints numbers from rows down to 1 on every row.
A single condition i == j replaces one position with *.
The star forms a diagonal from the top-right to the bottom-left.
This is a great exercise for combining loop control with simple conditional logic.
❓ Frequently Asked Questions
i == j. With j counting down from rows to 1, the star shifts one step left per row: 5432*, 543*1, ..., *4321.if (i + j == rows + 1) instead of if (i == j).rows = 9 (or read it with cin). The inner loop will then print from 9 down to 1 with one position replaced by *.Explore More C++ Number Patterns!
Try mixing numbers with symbols to create diagonals, borders, and matrix-like patterns.
Replacing a character based on a condition like i == j is the same idea used to print matrix diagonals. Once you understand it, you can create X-shapes, borders, and hollow patterns.
12 people found this page helpful
