A number-star diamond grows from 1 to a peak row of alternating digits and *, then mirrors back down — total lines = 2n − 1.
Remember
Rule: top i = 1..n, bottom i = n-1..1
each row: 2*i-1 chars, odd j → i, even j → *
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1 ← n = 5
Unlike Program 30 (right-aligned descending digits only), here two outer loops and j % 2 build a symmetric digit–star diamond.
Approach
How to Solve It
Print the top half i = 1..n, then the bottom half i = n−1..1. Each row alternates digit and star with modulus.
Method
Idea
Best for
Two outer loops + if/else
Top grows, bottom shrinks; j % 2 picks digit or *
Learning, interviews, exams
Ternary form
Same logic with j % 2 == 0 ? "*" : i
Shorter code once the idea clicks
Pseudocode
Pseudocode
for i from 1 to n:
for j from 1 to i*2 - 1:
print ("*" if j % 2 == 0 else i)
print newline
for i from n - 1 down to 1:
for j from 1 to i*2 - 1:
print ("*" if j % 2 == 0 else i)
print newline
Cheat sheet
Goal
Pattern
Top half
for (int i = 1; i <= n; i++)
Bottom half
for (int i = n - 1; i >= 1; i--)
Row length
for (int j = 1; j < i * 2; j++)
Digit or star
if (j % 2 == 0) print("*"); else print(i);
End the row
System.out.println();
Printing Numbers vs Starting a New Line
API
Effect
Use for
System.out.print
Stays on the same line
Each digit or *
System.out.println
Ends the current line
After the inner loop finishes
Print characters without a newline, then end the row once.
Try it
Live Preview
Change the height and the number-star diamond updates instantly.
Whole numbers from 3 to 7. Tap a chip or type a value — the preview redraws as you go.
Live resultn = 5 · 9 lines
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
Trace
Worked Walkthrough — n = 5 (top half)
Trace row length 2×i−1 and how j % 2 picks digit or star. The bottom half reuses the same row logic for i = 4..1.
i
Chars
Sequence
Printed row
1
1
1
1
2
3
2 * 2
2*2
3
5
3 * 3 * 3
3*3*3
4
7
4 * 4 * 4 * 4
4*4*4*4
5
9
5 * … * 5
5*5*5*5*5
Odd j prints i; even j prints *. Peak appears once; bottom half does not reprint it.
Code
Java Programs
Three complete programs: fixed n = 5, Scanner input with ternary form, and a compact dry-run. Use View Output to reveal sample results.
Example 1 — Fixed n = 5
Hard-coded height — if/else with j % 2 in both halves.
Java
public class NumberStarDiamond {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j < i * 2; j++) {
if (j % 2 == 0)
System.out.print("*");
else
System.out.print(i);
}
System.out.println();
}
for (int i = 4; i >= 1; i--) {
for (int j = 1; j < i * 2; j++) {
if (j % 2 == 0)
System.out.print("*");
else
System.out.print(i);
}
System.out.println();
}
}
}
Output
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1
How It Works
1. Top half grows.i runs from 1 to 5; each row has 2×i−1 characters.
2. Modulus alternates. Even j prints *; odd j prints i.
3. Bottom half mirrors. Second outer loop runs i = 4..1 — peak is not reprinted.
When i = 3: 3*3*3 (five characters).
Example 2 — Height Input
Read n at runtime. Ternary replaces if/else; both halves use n.
Java
import java.util.Scanner;
public class NumberStarDiamondInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter n: ");
int n = sc.nextInt();
if (n < 1) return;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i * 2; j++)
System.out.print(j % 2 == 0 ? "*" : i);
System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j < i * 2; j++)
System.out.print(j % 2 == 0 ? "*" : i);
System.out.println();
}
sc.close();
}
}
Output (when user enters 4)
Enter n: 4
1
2*2
3*3*3
4*4*4*4
3*3*3
2*2
1
How It Works
1. Prompt and guard. Read n; exit early if it is less than 1.
2. Generalize bounds. Top uses 1..n; bottom starts at n − 1.
3. Safer input tip. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive integer.");
return;
}
int n = sc.nextInt();
if (n < 1) {
System.out.println("Enter a positive integer.");
return;
}
Example 3 — Compact n = 3
Same if/else structure as Example 1 — smaller size for paper tracing.
Java
public class NumberStarDiamondSmall {
public static void main(String[] args) {
int n = 3;
for (int i = 1; i <= n; i++) {
for (int j = 1; j < i * 2; j++) {
if (j % 2 == 0) System.out.print("*");
else System.out.print(i);
}
System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j < i * 2; j++) {
if (j % 2 == 0) System.out.print("*");
else System.out.print(i);
}
System.out.println();
}
}
}
Output
1
2*2
3*3*3
2*2
1
How It Works
1. Same structure. Two outer loops + modulus — only n changes from 5 to 3.
2. Five lines total. Peak 3*3*3 appears once; bottom reprints 2*2 and 1.
3. Dry-run first. Trace i = 1..3 on paper before coding the full n = 5 demo.
Edge Cases & Pitfalls
Check these before calling the solution done.
println inside
Column of chars
If println is inside the inner loop, each character lands on its own line. Use print for digits/stars; println only after the inner loop.
Bottom from n
Double peak
Starting the bottom loop at n instead of n − 1 reprints the peak row.
Wrong modulus
Swapped chars
Using odd j for * (instead of even) starts each row with a star.
n = 1
Single digit
Output is just 1 — bottom loop never runs.
n ≤ 0
Empty output
Both outer loops never run — print nothing or show a message.
Bad input
Use hasNextInt
nextInt() throws on letters — prefer hasNextInt() and require a positive integer.
Analysis
Time and Space Complexity
Program
Time
Extra space
Number-star diamond (Examples 1–3)
O(n²)
O(1)
2n − 1 lines; peak row has 2n − 1 characters — total work is still quadratic. Only loop counters are stored.
Remember
Key Takeaways
Rule: top 1..n, bottom n−1..1; each row alternates i and * via j % 2.
Row length: each row prints 2×i−1 characters (j < i×2).
Break the row: call println only after the inner loop.
Complexity:O(n²) time; O(1) extra space.
One line: grow 1..n, shrink n−1..1; odd j → i, even j → *.
Frequently Asked Questions
The inner loop runs while j < i*2, which prints 1, 3, 5, 7, 9 characters for i = 1..5.
It checks j % 2. Even j prints '*'; odd j prints the current row number i.
The first loop builds the top half (i = 1..n). The second mirrors back down (i = n-1..1) to complete the diamond.
Program 30 is a right-aligned descending triangle. Program 31 alternates digits and stars in a symmetric diamond shape.
Replace 5 with n in both outer loops — see Example 2.
O(n²) for n rows because total printed characters grow quadratically across both halves.
Use sc.hasNextInt() before sc.nextInt() — see Example 2 notes.