Java Descending Number Pattern (Diagonal Asterisk)

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

What Is This Pattern?

A descending number pattern with a diagonal asterisk prints digits from n down to 1 on every row, but swaps one digit for * where the row index equals the column value.

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

As i grows, the star slides left. Natural step after Program 25 (bidirectional triangle).

How to Solve It

Descend j each row; swap with * when i == j.

MethodIdeaBest for
i == j swapPrint * on the match; else print jLearning, interviews, exams
Custom symbolSame loops; print # (or any char) instead of *When the diagonal marker should change

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 (int i = 1; i <= n; i++)
Descend columnsfor (int j = n; j >= 1; j--)
Diagonal starif (i == j) System.out.print("*");
Else digitelse System.out.print(j);
End the rowSystem.out.println();
Custom markerPrint "#" (or any string) instead of "*"

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach digit and the diagonal *
System.out.printlnEnds the current lineAfter the inner loop

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

Live Preview

Change the size n and the diagonal asterisk pattern updates instantly.

Whole numbers from 3 to 9 (single digits keep the shape clean). Tap a chip or type a value.

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

Worked Walkthrough — n = 4

Trace each row’s j sequence and where i == j places the star.

ij runStar atPrinted row
14 3 2 1j = 1432*
24 3 2 1j = 243*1
34 3 2 1j = 34*21
44 3 2 1j = 4*321

Each of n rows prints n characters — total n² prints, so time is O(n²).

Java Programs

Three complete programs: fixed n = 5, custom diagonal symbol, and Scanner input. Use View Output to reveal sample results.

Example 1 — Fixed n = 5

Hard-coded size — descend j; print * when i == j.

Java
public class DiagonalAsterisk {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 5; j >= 1; j--) {
                if (i == j)
                    System.out.print("*");
                else
                    System.out.print(j);
            }
            System.out.println();
        }
    }
}

How It Works

1. Outer loop walks rows. i runs from 1 to 5 — that value is where the star appears.

2. Inner loop descends. j runs from 5 down to 1 so digits print high-to-low.

3. Conditional swap. When i == j, print *; otherwise print j.

When i = 1, the star lands on j = 1 at the right end: 5432*. When i = 5, it lands on j = 5 at the left: *4321.

Example 2 — Custom Symbol #

Keep n = 5 but use hash instead of asterisk on the diagonal.

Java
public class DiagonalHash {
    public static void main(String[] args) {
        int n = 5;

        for (int i = 1; i <= n; i++) {
            for (int j = n; j >= 1; j--) {
                if (i == j)
                    System.out.print("#");
                else
                    System.out.print(j);
            }
            System.out.println();
        }
    }
}

How It Works

1. Same loops. Bounds and the i == j test match Example 1.

2. Different marker. Only the true-branch string changes from "*" to "#".

3. Same shape. Digits and diagonal position are unchanged — try spaces or other single characters the same way.

Example 3 — Size Input

Read n at runtime. Prefer hasNextInt() before nextInt() in real apps.

Java
import java.util.Scanner;

public class DiagonalAsteriskInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter size: ");
        int n = sc.nextInt();

        for (int i = 1; i <= n; i++) {
            for (int j = n; j >= 1; j--) {
                if (i == j)
                    System.out.print("*");
                else
                    System.out.print(j);
            }
            System.out.println();
        }
        sc.close();
    }
}

How It Works

1. Prompt and read. Ask for a size, then store it with sc.nextInt().

2. Same i == j core. Only the source of n changes — the diagonal scales with input.

3. Safer input tip. Prefer:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a positive whole number.");
    return;
}
int n = sc.nextInt();
if (n < 1) {
    System.out.println("Enter a positive whole number.");
    return;
}

Edge Cases & Pitfalls

Check these before calling the solution done.

Forward j

Ascending digits

Scanning j from 1 to n prints 1..n with a rising diagonal. Keep j descending for this shape.

Wrong test

Anti-diagonal

Using i + j == n + 1 places the star on the other diagonal. Stick to i == j for this pattern.

println inside

Column of chars

If println is inside the inner loop, each character lands on its own line. Use print for digits and stars; println only after the inner loop.

n = 1

Single *

Output is just * — a good sanity check.

n > 9

Multi-digit

Values like 10 print as two characters and skew the visual grid. Keep n ≤ 9 for clean single-digit columns.

Bad input

Use hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require a positive whole number.

Time and Space Complexity

ProgramTimeExtra space
Diagonal swap (Examples 1–3)O(n²)O(1)

Each of n rows prints exactly n characters — total n² prints, still quadratic in n.

Key Takeaways

  • Rule: for each row i, scan j from n down to 1 — print * when i == j.
  • Descending j: that is what creates the n..1 digit order on every row.
  • Break the row: call println only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: descend j, print * when i == j, else print j, then println().

Frequently Asked Questions

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 (5, 4, 3, 2, 1). When i != j, print that digit to fill the row.
Yes. Replace System.out.print("*") with any character or string — see Example 2 with #.
System.out.print stays on the same line for each digit or star. System.out.println 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.
Replace 5 with n in both loops — see Example 3 and the live preview.
O(n²) for n rows because each row prints n characters using a nested loop.
Use sc.hasNextInt() before sc.nextInt(), require n ≥ 1, and reject non-numeric input — see Example 3 notes.

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

Move on to the palindrome number triangle in the Java number-pattern series.

Program 27 tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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