Diamond Pattern in Java
Print a diamond of stars for a given half height n.
- Time
O(n^2) - Space
O(1) - Hints3
- Read3 min
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.
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:
*
***
*****
*******
*****
***
*
A whole number n.
A diamond with 2n - 1 rows.
Examples
n = 2 *
***
*Rows 1, 2 and 1: one star, three stars, one star.
n = 1*The top half is one row and there is no bottom half.
Constraints
- 1 <= n <= 50
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?
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
- Write a method that prints one row:
n - rowspaces, then2 * row - 1stars. - Call it for
row = 1ton. This is the top half with the middle row. - Call it for
row = n - 1down to1. 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.
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
Solution
Reveal the full Java solution
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.
O(n^2)O(1)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
FizzBuzz Problem
Hints · Dry run · SolutionSolve Similar problemPrint the Fibonacci Series Up to N Terms
Hints · Dry run · SolutionSolve Similar problemFind All Factors of a Number
Hints · Dry run · SolutionSolve Related learningLoops
TutorialLearn Related learningControl Statements
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartPractising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
