Filled Diamond Star Pattern in Java

Beginner
⏱️ 9 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
2n − 1 rows total

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:

Output
    *    
   ***   
  *****  
 ******* 
*********
 ******* 
  *****  
   ***   
    *    
1

Complete Java Program

Fixed rows = 5 version:

Java
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

1

Upper half (i = 1rows)

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.

Pyramid
2

Lower half (i = rows - 11)

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.

Mirror
3

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.

Odd counts
4

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).

Line break
=

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.

2

Variation — User Input Version

Accept rows with Scanner:

Java
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 StringBuilder for large rows
  • 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

1

A filled diamond uses two halves: increasing odd stars then decreasing odd stars.

2

Each row prints rows - i spaces and 2 * i - 1 stars.

3

Total output lines are 2 * rows - 1.

4

Time complexity is O(n²) for n rows.

5

This is the filled version of the hollow diamond (Program 9).

❓ Frequently Asked Questions

Odd star counts keep the diamond centered around one middle column. Each next row changes star count by 2, expanding or shrinking evenly on both sides.
The top half already prints the widest row when i == rows. Starting at rows - 1 prevents printing the middle row twice.
It’s O(n²) because there are Theta(n) lines and each line prints Theta(n) characters.

Next: Diamond in a Frame

Continue to Program 11 to print a diamond inside a square-style frame.

Program 11 →
Did you know?

A filled diamond is basically a pyramid followed by an inverted pyramid, with the middle row printed only once.

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.

10 people found this page helpful