Easy Logical Thinking Java

Diamond Pattern in Java

Print a diamond of stars for a given half height n.

  • TimeO(n^2)
  • SpaceO(1)
  • Hints3
  • Read3 min
Quick answer

To print a diamond pattern in Java, print rows 1 to n and then rows n - 1 down to 1. In each row print n - row spaces followed by 2 * row - 1 stars. The first group makes the top half and the second group makes the bottom half.

Nested loopsPatternsSymmetry
1

Problem statement

Print a diamond pattern in Java using stars. The number n is the height of the top half, with the middle row included. The full diamond has 2n - 1 rows.

For n = 4:

   *
  ***
 *****
*******
 *****
  ***
   *
Input

A whole number n.

Output

A diamond with 2n - 1 rows.

Examples

Example 1
Inputn = 2
Output * *** *

Rows 1, 2 and 1: one star, three stars, one star.

Example 2
Inputn = 1
Output*

The top half is one row and there is no bottom half.

Constraints

  • 1 <= n <= 50
2

Think before you code

Reveal the questions to ask yourself first
  • How many spaces and how many stars are in row 1, row 2 and row 3?
  • Which row is the widest?
  • Can the bottom half reuse the code of the top half?
3

Hints

Open one at a time — try again after each.
1 Hint 1
Stars in a row: 1, 3, 5, 7. That is 2 * row - 1.
2 Hint 2
Spaces before the stars: n - row.
3 Hint 3
For the bottom half, run the same row code from n - 1 down to 1.
4

Approach

Reveal the step-by-step approach
  1. Write a method that prints one row: n - row spaces, then 2 * row - 1 stars.
  2. Call it for row = 1 to n. This is the top half with the middle row.
  3. Call it for row = n - 1 down to 1. This is the bottom half.

You only need spaces on the left, because nothing is printed on the right. If nested loops feel hard, revise loops in Java and begin with easier star patterns.

5

Dry run

Walk through the example step by step

For n = 4:

row | spaces (4 - row) | stars (2 * row - 1)
----+------------------+--------------------
1   | 3                | 1
2   | 2                | 3
3   | 1                | 5
4   | 0                | 7
3   | 1                | 5
2   | 2                | 3
1   | 3                | 1
6

Solution

Reveal the full Java solution
Solution.java
public class DiamondPattern {
    static void printRow(int n, int row) {
        for (int s = 1; s <= n - row; s++) {
            System.out.print(" ");
        }
        for (int k = 1; k <= 2 * row - 1; k++) {
            System.out.print("*");
        }
        System.out.println();
    }

    public static void printDiamond(int n) {
        for (int row = 1; row <= n; row++) {
            printRow(n, row); // top half, middle row included
        }
        for (int row = n - 1; row >= 1; row--) {
            printRow(n, row); // bottom half
        }
    }

    public static void main(String[] args) {
        printDiamond(4);
    }
}

This is the diamond pattern in Java using for loop. One row method serves both halves, so there is no repeated code. The middle row is printed once because the second loop starts at n - 1.

Hollow diamond pattern in Java

Here one loop runs from -(n - 1) to n - 1. The row number is n - Math.abs(i), which goes up and then down by itself. Print a star only at the first and last place.

static void hollowDiamond(int n) {
    for (int i = -(n - 1); i <= n - 1; i++) {
        int row = n - Math.abs(i);
        for (int s = 1; s <= n - row; s++) System.out.print(" ");
        int width = 2 * row - 1;
        for (int k = 1; k <= width; k++) {
            boolean border = (k == 1 || k == width);
            System.out.print(border ? "*" : " ");
        }
        System.out.println();
    }
}

Output for n = 4:

   *
  * *
 *   *
*     *
 *   *
  * *
   *

Diamond pattern in Java using numbers

Print the digits up to the row number, then back down to 1.

static void numberDiamond(int n) {
    for (int i = -(n - 1); i <= n - 1; i++) {
        int row = n - Math.abs(i);
        for (int s = 1; s <= n - row; s++) System.out.print(" ");
        for (int k = 1; k <= row; k++) System.out.print(k);
        for (int k = row - 1; k >= 1; k--) System.out.print(k);
        System.out.println();
    }
}

Output for n = 4:

   1
  121
 12321
1234321
 12321
  121
   1

This shape stays neat up to n = 9. From 10 on, numbers take two places.

Practise next

Change the diamond pattern in Java code to take n from a Scanner. Then try more pattern programs and the Pascal triangle. For guided practice, see the Java full stack course.

TimeO(n^2)
SpaceO(1)
7

Review before the interview

Common mistakes

  • Printing the middle row twice by starting the second loop at n.
  • Printing row stars instead of 2 * row - 1, which makes the sides uneven.
  • Calling println for each star, which puts every star on a new line.

Edge cases to test

  • n = 1 prints one star.
  • n = 0 prints nothing.
  • In the hollow version, the top and bottom rows have one star only.

Interview follow-ups

  • How would you print a half diamond?
  • How would you print the diamond with letters?
  • How would you print a diamond when the total number of rows is given?

Keep practising

Practising for Java interviews?

CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.

Chat with us