Java Descending Number Pattern (Diagonal Asterisk)
Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview
Definition
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).
Approach
How to Solve It
Descend j each row; swap with * when i == j.
Method
Idea
Best for
i == j swap
Print * on the match; else print j
Learning, interviews, exams
Custom symbol
Same 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
Goal
Pattern
Walk rows
for (int i = 1; i <= n; i++)
Descend columns
for (int j = n; j >= 1; j--)
Diagonal star
if (i == j) System.out.print("*");
Else digit
else System.out.print(j);
End the row
System.out.println();
Custom marker
Print "#" (or any string) instead of "*"
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit and the diagonal *
System.out.println
Ends the current line
After the inner loop
Print characters without a newline, then end the row once.
Try it
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 resultn = 5 · 25 characters
5432*
543*1
54*21
5*321
*4321
Trace
Worked Walkthrough — n = 4
Trace each row’s j sequence and where i == j places the star.
i
j run
Star at
Printed row
1
4 3 2 1
j = 1
432*
2
4 3 2 1
j = 2
43*1
3
4 3 2 1
j = 3
4*21
4
4 3 2 1
j = 4
*321
Each of n rows prints n characters — total n² prints, so time is O(n²).
Code
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();
}
}
}
Output
5432*
543*1
54*21
5*321
*4321
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();
}
}
}
Output
5432#
543#1
54#21
5#321
#4321
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();
}
}
Output (when user enters 4)
Enter size: 4
432*
43*1
4*21
*321
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.
Analysis
Time and Space Complexity
Program
Time
Extra 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.
Remember
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.