C++ Descending Number Pattern (Diagonal Asterisk)

Beginner
5 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

Each row prints digits from n down to 1, but when i == j it prints * instead — a diagonal asterisk that moves left each row.

Remember
Rule: for i from 1 to n
        for j from n down to 1
          print * if i == j, else print j

5432*
543*1
54*21
5*321
*4321     ← n = 5

Follows the bidirectional triangle in Program 25; next is the palindrome number triangle in Program 27.

How to Solve It

Outer loop grows i. Reverse inner loop prints descending j, swapping to * when i == j.

MethodIdeaBest for
i == j swapPrint * on the diagonal; otherwise print descending jLearning, interviews, exams
Custom symbolReplace "*" with "#" or any characterSame shape, different marker

Pseudocode

Pseudocode
for i from 1 to n:
    for j from n down to 1:
        if i == j:
            print "*"
        else:
            print j
    print newline

Cheat sheet

GoalPattern
Walk rowsfor (i = 1; i <= n; i++)
Descend columnsfor (j = n; j >= 1; j--)
Diagonal starif (i == j) cout << "*";
Else digitelse cout << j;
End of rowcout << "\n";

Printing Numbers vs Starting a New Line

APIEffectUse for
cout << j / "*"Stays on the same lineEach digit or star on the row
cout << "\n"Ends the current lineAfter the inner loop finishes

Print characters without a newline, then end the row once.

Live Preview

Change the size n and the diagonal asterisk pattern updates instantly — capped at 9 for readable demos.

Whole numbers from 1 to 9. Tap a chip or type a value — the preview redraws as you go.

Live result n = 5 · 25 chars
5432*
543*1
54*21
5*321
*4321

Worked Walkthrough — n = 5

Trace how i == j places the star while the rest of the row stays descending digits.

iStar at jSequencePrints
11 (right)5 4 3 2 *5432*
225 4 3 * 1543*1
335 4 * 2 154*21
445 * 3 2 15*321
55 (left)* 4 3 2 1*4321

Each of n rows prints n characters → n² prints → O(n²).

C++ Programs

Three complete programs: fixed n = 5, cin input, and a compact n = 3 demo. Use View Output to reveal sample results.

Example 1 — Fixed n = 5

Hard-coded size — descending j, print * when i == j.

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

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

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

    return 0;
}

How It Works

1. Outer sets the star column. i is both the row index and where * appears.

2. Inner descends. j runs from n down to 1 for every row.

3. Swap on diagonal. When i == j print *; otherwise print j.

Example 2 — User Input Size

Read n with cin, validate, then use the same i == j core.

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

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

    cout << "Enter size: ";
    if (!(cin >> n) || n <= 0)
    {
        cout << "Please enter a positive integer.\n";
        return 1;
    }

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

    return 0;
}

How It Works

1. Prompt and validate. Reject failed reads and non-positive values before printing.

2. Same core. Diagonal swap matches Example 1 — only n comes from the user.

3. Safer input tip. Cap demos for readable output:

Safer input
if (!(cin >> n) || n < 1 || n > 9)
{
    cout << "Enter a whole number from 1 to 9.\n";
    return 1;
}

Example 3 — Compact n = 3

Same structure with only three rows — easy to confirm the star moving left.

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

int main()
{
    int n = 3;
    int i, j;

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

    return 0;
}

How It Works

1. Three rows. i = 1 → 32*; i = 2 → 3*1; i = 3 → *21.

2. Trace on paper. If you flip the condition to i != j, stars appear everywhere except the diagonal.

3. Scale up next. Once the small demo is clear, use Examples 1–2 for n = 5 or user input.

Edge Cases & Pitfalls

Check these before calling the solution done.

i != j

Flipped condition

Printing * when i != j fills almost the whole grid with stars. Keep i == j for the diagonal only.

j++

Ascending inner loop

Using j = 1..n prints ascending digits. For this shape keep j = n..1.

\n inside

Broken rows

If cout << "\n" sits inside the inner loop, each character lands on its own line. Call it only after the row finishes.

n = 1

Single star

Output is just * — a good sanity check for input validation.

cout << i

Wrong digit

Printing i instead of j fills each row with the same digit. Print the column variable j.

cin

Check the stream

Validate cin >> n before looping — a failed read leaves n unset or stale.

Time and Space Complexity

ProgramTimeExtra space
Fixed / input (Examples 1–2)O(n²)O(1)
Compact n = 3 (Example 3)O(n²)O(1)

Each of n rows prints exactly n characters → n² prints → O(n²) time. Only a few loop variables are needed.

Key Takeaways

  • Descend with j: inner loop runs j = n..1 every row.
  • Diagonal rule: print * when i == j; otherwise print j.
  • Break the row: digits/stars in the inner loop; cout << "\n" after it.
  • Complexity: O(n²) time from n² characters; O(1) extra space.

One line: for each row i, scan j from n to 1 — print * if i == j, else j, then newline.

Frequently Asked Questions

A descending digit grid with one diagonal asterisk: for n=5 you get 5432* / 543*1 / 54*21 / 5*321 / *4321 — the star moves left each row.
Because i increases from 1 to n while j decreases from n to 1. The condition i == j becomes true at a different position each row.
j holds the descending column digit. When i != j, print that digit to fill the row.
Yes. Replace cout << "*" with any character such as # or X.
cout << j prints each digit or star on the same line. cout << "\n" ends the row after the inner loop finishes.
j runs from n down to 1 so each row prints digits in descending order with the star at position i.
After prompting, use if (!(cin >> n) || n <= 0) to reject bad input before printing.
O(n²) for n rows because each row prints n characters using a nested loop.

Did you know?

This pattern prints descending numbers from n to 1 on each row. When the row index equals the current column value (i == j), it prints * instead of the number, creating a diagonal asterisk that moves left each row.

Next: Palindrome Number Triangle

Continue with rows that read the same forwards and backwards.

Program 27 tutorial →

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