Descending Numbers with Diagonal Star in C++

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

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:

Output
5432*
543*1
54*21
5*321
*4321
1

Complete C++ Program

The inner loop prints rows..1 each row, and the diagonal value is replaced with *.

C++
#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

1

Set the size

int rows = 5; defines both the number of rows and the range of numbers printed.

Setup
2

Outer loop (row index)

for (i = 1; i <= rows; i++) selects which row you’re printing.

Row control
3

Inner loop (descending print)

for (j = rows; j >= 1; j--) prints the sequence rows..1 on each line.

Number printing
4

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.

Condition
=

Diagonal star pattern

Each row prints rows characters, so total work is O(n²) for n rows.

2

Variation — User Input Version

Let the user decide the number of rows 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 = 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 using rows
  • 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 j counting down)
  • Forgetting the newline after each row
  • Hard-coding 5 in multiple places instead of using rows
  • Using endl inside loops unnecessarily

Key Takeaways

1

The inner loop prints numbers from rows down to 1 on every row.

2

A single condition i == j replaces one position with *.

3

The star forms a diagonal from the top-right to the bottom-left.

4

This is a great exercise for combining loop control with simple conditional logic.

❓ Frequently Asked Questions

On the position where i == j. With j counting down from rows to 1, the star shifts one step left per row: 5432*, 543*1, ..., *4321.
Use if (i + j == rows + 1) instead of if (i == j).
Yes. Set rows = 9 (or read it with cin). The inner loop will then print from 9 down to 1 with one position replaced by *.
O(n²) for n rows because you print n characters on each of n lines.

Explore More C++ Number Patterns!

Try mixing numbers with symbols to create diagonals, borders, and matrix-like patterns.

All Number Patterns →
Did you know?

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.

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