Filled Diamond Star Pattern in Java

What You'll Learn
This program prints a filled diamond using two halves: the upper half increases stars by 2 each row, and the lower half decreases stars by 2, keeping the pattern centered with leading spaces.
For a given row index i, the star count is 2 * i - 1. Total output lines are 2 * rows - 1.
⭐ Pattern Output
When you run the program with rows = 5:
*
***
*****
*******
*********
*******
*****
***
* Complete Java Program
Fixed rows = 5 version:
public class Main {
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 j = 1; j <= 2 * i - 1; j++) 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 j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.println();
}
}
}🧠 How It Works
Upper half (i = 1 … rows)
for (int i = 1; i <= rows; i++) builds the top half: for (int j = 1; j <= rows - i; j++) System.out.print(" "); then for (int j = 1; j <= 2 * i - 1; j++) System.out.print("*");, then System.out.println(). Star counts are 1, 3, 5, …, 2*rows-1.
Lower half (i = rows - 1 … 1)
for (int i = rows - 1; i >= 1; i--) uses the same two inner loops. As i shrinks, rows - i grows (more margin) and 2 * i - 1 shrinks (fewer stars)—for rows = 5 the lower star runs are 7, 5, 3, 1.
Why 2 * i - 1?
Odd widths keep a single center star on each row. Each full row prints (rows - i) + (2i - 1) = rows + i - 1 characters before the newline.
New line every row
System.out.println() after the star loop ends one row of the diamond. Both outer passes use the same three-step pattern (spaces, stars, newline).
Solid diamond
2 * rows - 1 lines total; widest line has 2 * rows - 1 stars. O(n²) characters for n = rows, O(1) extra space. The green preview scrolls sideways on phones when the middle row is wider than the viewport.
Variation — User Input Version
Accept rows with Scanner:
import java.util.Scanner;
public class Main {
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 j = 1; j <= 2 * i - 1; j++) 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 j = 1; j <= 2 * i - 1; j++) System.out.print("*");
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Print a hollow diamond by printing stars only on the edges (Program 9)
- Use a different character (like
#) - Use
StringBuilderfor largerows - Validate input before printing
- Experiment with printing spaces between stars
Avoid
- Duplicating the middle row (start the lower half from
rows - 1) - Mixing tabs and spaces for alignment
- Forgetting newline after each row
- Using even star counts (symmetry breaks)
- Assuming user input is always valid
Key Takeaways
A filled diamond uses two halves: increasing odd stars then decreasing odd stars.
Each row prints rows - i spaces and 2 * i - 1 stars.
Total output lines are 2 * rows - 1.
Time complexity is O(n²) for n rows.
This is the filled version of the hollow diamond (Program 9).
❓ Frequently Asked Questions
i == rows. Starting at rows - 1 prevents printing the middle row twice.Next: Diamond in a Frame
Continue to Program 11 to print a diamond inside a square-style frame.
A filled diamond is basically a pyramid followed by an inverted pyramid, with the middle row printed only once.
10 people found this page helpful
