Centered Number Diamond in Java

Beginner
⏱️ 5 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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:

Output
    1
   123
  12345
 1234567
123456789
 1234567
  12345
   123
    1
1

Complete Java Program

Print the top half from i=1 to rows, then print the mirrored bottom half from rows-1 down to 1.

Java
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

1

Choose the size

rows = 5 defines the height of the top half (and the widest line).

Setup
2

Top half grows the width

For each row i, the program prints 2*i-1 digits (1, 123, 12345, ...).

Top half
3

Spaces center-align each row

Leading spaces are rows - i. Smaller rows get more spaces, so the shape is centered.

Alignment
4

Bottom half mirrors the pattern

A second loop counts i down from rows-1 to 1 to print the mirrored lower half.

Mirror
=

Centered number diamond

The diamond uses odd-width rows. Total prints grow on the order of O(n²) as n increases.

2

Variation — User Input Version

Let the user choose the number of rows using 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 = 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 rows to make a larger diamond
  • Use StringBuilder to 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.in with Scanner if you need console input later in the same JVM run

Key Takeaways

1

Each row prints odd counts of digits: 2*i-1.

2

Leading spaces (rows-i) keep the diamond centered.

3

The bottom half is a mirror of the top half (rows-1..1).

4

Nested loops make it easy to generate symmetric console patterns.

❓ Frequently Asked Questions

On the last row i=5, the loop runs while k < 2*i, so it prints k = 1..9 (9 digits).
Yes. Print k + " " inside the digit loop. You may also need to adjust indentation to keep centering.
Print an increasing loop up to i and then a decreasing loop back to 1 on each row. That creates the mirrored numbers within the row.
O(n²) for n rows due to nested loops across the diamond.

Explore More Java Number Patterns!

Diamonds combine alignment and symmetry—great practice for nested loops.

All Number Patterns →
Did you know?

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.

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.

12 people found this page helpful