Centered Number Diamond in Java

What You’ll Learn
How to print a centered number diamond in Java where each row prints:
1, then 123, then 12345, and so on—then mirrors back down to 1.
You’ll combine two parts: the top half (increasing) and the bottom half (decreasing), using nested loops and leading spaces.
⭐ Pattern Output
For rows = 5, the pattern looks like this:
1
123
12345
1234567
123456789
1234567
12345
123
1Complete Java Program
Print the top half from i=1 to rows, then print the mirrored bottom half from rows-1 down to 1.
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
for (int k = 1; k < i * 2; k++) {
System.out.print(k);
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (int k = 1; k < i * 2; k++) {
System.out.print(k);
}
System.out.println();
}
}
}🧠 How It Works
Choose the size
rows = 5 defines the height of the top half (and the widest line).
Top half grows the width
For each row i, the program prints 2*i-1 digits (1, 123, 12345, ...).
Spaces center-align each row
Leading spaces are rows - i. Smaller rows get more spaces, so the shape is centered.
Bottom half mirrors the pattern
A second loop counts i down from rows-1 to 1 to print the mirrored lower half.
Centered number diamond
The diamond uses odd-width rows. Total prints grow on the order of O(n²) as n increases.
Variation — User Input Version
Let the user choose the number of rows using 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 = i; j < rows; j++) {
System.out.print(" ");
}
for (int k = 1; k < i * 2; k++) {
System.out.print(k);
}
System.out.println();
}
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (int k = 1; k < i * 2; k++) {
System.out.print(k);
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Print spaces between digits (e.g.,
System.out.print(k + " ")) - Make a palindromic diamond (1 2 3 2 1) by printing up and then down per row
- Replace digits with characters to create an alphabet diamond
- Increase
rowsto make a larger diamond - Use
StringBuilderto build each row if you want more complex formatting
Avoid
- Forgetting the bottom half (you’ll only get a pyramid, not a diamond)
- Printing inconsistent indentation (the shape won’t be centered)
- Hard-coding 5 everywhere instead of using
rows - Closing
System.inwithScannerif you need console input later in the same JVM run
Key Takeaways
Each row prints odd counts of digits: 2*i-1.
Leading spaces (rows-i) keep the diamond centered.
The bottom half is a mirror of the top half (rows-1..1).
Nested loops make it easy to generate symmetric console patterns.
❓ Frequently Asked Questions
i=5, the loop runs while k < 2*i, so it prints k = 1..9 (9 digits).k + " " inside the digit loop. You may also need to adjust indentation to keep centering.i and then a decreasing loop back to 1 on each row. That creates the mirrored numbers within the row.Explore More Java Number Patterns!
Diamonds combine alignment and symmetry—great practice for nested loops.
Many diamond patterns are just two triangles: an increasing top half and a decreasing bottom half. Once you learn one, you can build lots of variations.
12 people found this page helpful
