A filled diamond star pattern is a centered solid diamond: an upper pyramid of odd-length star rows, mirrored below, with the widest row printed only once.
Remember
Rule: spaces = rows - i, stars = 2 * i - 1
Grow i to rows, then shrink from rows - 1
*
***
*****
*******
*********
*******
*****
***
* ← rows = 5 (half-height)
In Java you print each row with two inner loops (spaces, then stars), then System.out.println(). Run that once upward and once downward so the shape closes into a diamond.
Approach
How to Solve It
Reuse one row formula for both halves — start with nested loops, then optionally shorten with String.repeat (Java 11+).
Method
Idea
Best for
Nested loops
Spaces loop + stars loop, twice (up then down)
Learning, interviews, exams
String.repeat
Build spaces and stars as whole strings
Shorter demos once loops click
Pseudocode
Pseudocode
for i from 1 to rows: // upper half
print (rows - i) spaces
print (2 * i - 1) stars
print newline
for i from rows - 1 down to 1: // lower half
print (rows - i) spaces
print (2 * i - 1) stars
print newline
Three complete programs: fixed half-height, Scanner input, and a String.repeat helper. Use View Output to reveal sample results.
Example 1 — Fixed rows = 5
Hard-coded half-height — ideal for first demos and screenshots.
Java
class FilledDiamond {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
Output
*
***
*****
*******
*********
*******
*****
***
*
How It Works
1. Set half-height.rows = 5 means the diamond peaks at 5 stars wide on each side of center — 9 total lines.
2. Upper half.i runs from 1 to rows. Print rows - i spaces, then 2 * i - 1 stars, then a newline.
3. Lower half.i runs from rows - 1 down to 1 with the same two inner loops — so the widest row is not repeated.
4. Break the line.System.out.println() after both inner loops starts the next row.
Example 2 — User Input Version
Read the half-height at runtime with Scanner. Prefer hasNextInt in real apps (shown in the tip below).
Java
import java.util.Scanner;
class FilledDiamondInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--) {
for (int j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
sc.close();
}
}
Output (when user enters 4)
Enter the number of rows: 4
*
***
*****
*******
*****
***
*
How It Works
1. Prompt and read. Ask for a row count, then sc.nextInt() stores the integer.
2. Same two-phase core. Only the source of rows changes — the print logic matches Example 1.
3. Safer input tip.nextInt() throws on letters. Prefer:
Safer input
if (!sc.hasNextInt()) {
System.out.println("Enter a positive whole number.");
return;
}
int rows = sc.nextInt();
if (rows < 1) {
System.out.println("Enter a positive whole number.");
return;
}
Example 3 — String.repeat Helper (Java 11+)
Build each row in two calls — same shape, no explicit space/star inner loops.
Java
class FilledDiamondRepeat {
static void printRow(int rows, int i) {
System.out.print(" ".repeat(rows - i));
System.out.println("*".repeat(2 * i - 1));
}
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
printRow(rows, i);
}
for (int i = rows - 1; i >= 1; i--) {
printRow(rows, i);
}
}
}
Output
*
***
*****
*******
*********
*******
*****
***
*
How It Works
1. One helper for a row.printRow writes rows - i spaces, then a line of 2 * i - 1 stars.
2. Same outer structure. Call it upward for i = 1..rows, then downward from rows - 1.
3. Learn loops first. Use Examples 1–2 when you need to show nested bounds; treat this as a polish shortcut afterward.
Edge Cases & Pitfalls
Check these before calling the solution done.
Lower at rows
Doubled middle
If the lower loop starts at rows, the widest line prints twice. Start at rows - 1.
Even stars
Lost center
Use 2 * i - 1 (odd counts). Even widths break left–right symmetry.
Wrong spaces
Not centered
Spaces must be rows - i. Using i spaces leans the diamond the wrong way.
rows = 1
Single star
Upper prints *; lower never runs — correct tiny diamond.
rows ≤ 0
Empty output
Both outer loops skip. Validate and re-prompt for interactive programs.
Bad input
Check hasNextInt
nextInt() throws on letters — prefer sc.hasNextInt() first.
Analysis
Time and Space Complexity
Program
Time
Extra space
Nested loops (Examples 1–2)
O(rows²)
O(1)
String.repeat helper (Example 3)
O(rows²)
O(rows) per temporary row string
About 2 * rows - 1 lines; each line does Θ(rows) work for spaces and stars. Total stars = rows² + (rows - 1)².
Remember
Key Takeaways
Row formula:rows - i spaces and 2 * i - 1 stars.
Two phases: grow i to rows, then shrink from rows - 1.
Break the row:print for spaces/stars; println after both inner loops.
Complexity:O(n²) time for half-height n; O(1) extra space for nested loops.
One line: spaces = rows - i, stars = 2*i - 1, grow then shrink from rows - 1.
Frequently Asked Questions
The first outer loop runs i from 1 to rows. On each row it prints (rows - i) spaces, then (2 * i - 1) stars. That builds the upper centered pyramid. The second outer loop runs i from (rows - 1) down to 1 with the same two inner loops, mirroring the shape so the diamond closes.
The first part already prints the widest row when i equals rows. Starting the second part at rows - 1 continues with the next narrower rows without repeating the middle line.
The filled diamond prints full runs of stars using 2*i-1 stars per row. The hollow diamond uses diagonal conditions so only the outline has stars. Both use an upper phase and a lower phase starting at rows - 1.
Odd widths keep a single center star on each row and grow by one star on each side per step, which keeps left–right symmetry.
With n equal to rows (half-height), there are 2n - 1 printed lines. Each line does Theta(n) work for spaces and stars combined, so overall time is O(n²).
Exactly 2 * rows - 1 lines. The widest line has 2 * rows - 1 stars.
Yes. Upper half is Program 5; lower half uses the same inner loops with i running like Program 6’s inverted idea, but starting at rows - 1.
Check sc.hasNextInt() before sc.nextInt() and require rows >= 1 so bad input does not throw InputMismatchException.
🤔
Did you know?
The filled diamond is Program 5’s pyramid plus its mirror: same (rows - i) spaces and (2 * i - 1) stars, with the lower half starting at rows - 1 so the widest row prints only once.