Java Number-Star Diamond Pattern

Beginner
7 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A number-star diamond grows from 1 to a peak row of alternating digits and *, then mirrors back down — total lines = 2n − 1.

Remember
Rule: top i = 1..n, bottom i = n-1..1
      each row: 2*i-1 chars, odd j → i, even j → *

1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1     ← n = 5

Unlike Program 30 (right-aligned descending digits only), here two outer loops and j % 2 build a symmetric digit–star diamond.

How to Solve It

Print the top half i = 1..n, then the bottom half i = n−1..1. Each row alternates digit and star with modulus.

MethodIdeaBest for
Two outer loops + if/elseTop grows, bottom shrinks; j % 2 picks digit or *Learning, interviews, exams
Ternary formSame logic with j % 2 == 0 ? "*" : iShorter code once the idea clicks

Pseudocode

Pseudocode
for i from 1 to n:
    for j from 1 to i*2 - 1:
        print ("*" if j % 2 == 0 else i)
    print newline
for i from n - 1 down to 1:
    for j from 1 to i*2 - 1:
        print ("*" if j % 2 == 0 else i)
    print newline

Cheat sheet

GoalPattern
Top halffor (int i = 1; i <= n; i++)
Bottom halffor (int i = n - 1; i >= 1; i--)
Row lengthfor (int j = 1; j < i * 2; j++)
Digit or starif (j % 2 == 0) print("*"); else print(i);
End the rowSystem.out.println();

Printing Numbers vs Starting a New Line

APIEffectUse for
System.out.printStays on the same lineEach digit or *
System.out.printlnEnds the current lineAfter the inner loop finishes

Print characters without a newline, then end the row once.

Live Preview

Change the height and the number-star diamond updates instantly.

Whole numbers from 3 to 7. Tap a chip or type a value — the preview redraws as you go.

Live result n = 5 · 9 lines
1
2*2
3*3*3
4*4*4*4
5*5*5*5*5
4*4*4*4
3*3*3
2*2
1

Worked Walkthrough — n = 5 (top half)

Trace row length 2×i−1 and how j % 2 picks digit or star. The bottom half reuses the same row logic for i = 4..1.

iCharsSequencePrinted row
1111
232 * 22*2
353 * 3 * 33*3*3
474 * 4 * 4 * 44*4*4*4
595 * … * 55*5*5*5*5

Odd j prints i; even j prints *. Peak appears once; bottom half does not reprint it.

Java Programs

Three complete programs: fixed n = 5, Scanner input with ternary form, and a compact dry-run. Use View Output to reveal sample results.

Example 1 — Fixed n = 5

Hard-coded height — if/else with j % 2 in both halves.

Java
public class NumberStarDiamond {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j < i * 2; j++) {
                if (j % 2 == 0)
                    System.out.print("*");
                else
                    System.out.print(i);
            }
            System.out.println();
        }
        for (int i = 4; i >= 1; i--) {
            for (int j = 1; j < i * 2; j++) {
                if (j % 2 == 0)
                    System.out.print("*");
                else
                    System.out.print(i);
            }
            System.out.println();
        }
    }
}

How It Works

1. Top half grows. i runs from 1 to 5; each row has 2×i−1 characters.

2. Modulus alternates. Even j prints *; odd j prints i.

3. Bottom half mirrors. Second outer loop runs i = 4..1 — peak is not reprinted.

When i = 3: 3*3*3 (five characters).

Example 2 — Height Input

Read n at runtime. Ternary replaces if/else; both halves use n.

Java
import java.util.Scanner;

public class NumberStarDiamondInput {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter n: ");
        int n = sc.nextInt();
        if (n < 1) return;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j < i * 2; j++)
                System.out.print(j % 2 == 0 ? "*" : i);
            System.out.println();
        }
        for (int i = n - 1; i >= 1; i--) {
            for (int j = 1; j < i * 2; j++)
                System.out.print(j % 2 == 0 ? "*" : i);
            System.out.println();
        }
        sc.close();
    }
}

How It Works

1. Prompt and guard. Read n; exit early if it is less than 1.

2. Generalize bounds. Top uses 1..n; bottom starts at n − 1.

3. Safer input tip. Prefer:

Safer input
if (!sc.hasNextInt()) {
    System.out.println("Enter a positive integer.");
    return;
}
int n = sc.nextInt();
if (n < 1) {
    System.out.println("Enter a positive integer.");
    return;
}

Example 3 — Compact n = 3

Same if/else structure as Example 1 — smaller size for paper tracing.

Java
public class NumberStarDiamondSmall {
    public static void main(String[] args) {
        int n = 3;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j < i * 2; j++) {
                if (j % 2 == 0) System.out.print("*");
                else System.out.print(i);
            }
            System.out.println();
        }
        for (int i = n - 1; i >= 1; i--) {
            for (int j = 1; j < i * 2; j++) {
                if (j % 2 == 0) System.out.print("*");
                else System.out.print(i);
            }
            System.out.println();
        }
    }
}

How It Works

1. Same structure. Two outer loops + modulus — only n changes from 5 to 3.

2. Five lines total. Peak 3*3*3 appears once; bottom reprints 2*2 and 1.

3. Dry-run first. Trace i = 1..3 on paper before coding the full n = 5 demo.

Edge Cases & Pitfalls

Check these before calling the solution done.

println inside

Column of chars

If println is inside the inner loop, each character lands on its own line. Use print for digits/stars; println only after the inner loop.

Bottom from n

Double peak

Starting the bottom loop at n instead of n − 1 reprints the peak row.

Wrong modulus

Swapped chars

Using odd j for * (instead of even) starts each row with a star.

n = 1

Single digit

Output is just 1 — bottom loop never runs.

n ≤ 0

Empty output

Both outer loops never run — print nothing or show a message.

Bad input

Use hasNextInt

nextInt() throws on letters — prefer hasNextInt() and require a positive integer.

Time and Space Complexity

ProgramTimeExtra space
Number-star diamond (Examples 1–3)O(n²)O(1)

2n − 1 lines; peak row has 2n − 1 characters — total work is still quadratic. Only loop counters are stored.

Key Takeaways

  • Rule: top 1..n, bottom n−1..1; each row alternates i and * via j % 2.
  • Row length: each row prints 2×i−1 characters (j < i×2).
  • Break the row: call println only after the inner loop.
  • Complexity: O(n²) time; O(1) extra space.

One line: grow 1..n, shrink n−1..1; odd j → i, even j → *.

Frequently Asked Questions

The inner loop runs while j < i*2, which prints 1, 3, 5, 7, 9 characters for i = 1..5.
It checks j % 2. Even j prints '*'; odd j prints the current row number i.
The first loop builds the top half (i = 1..n). The second mirrors back down (i = n-1..1) to complete the diamond.
Program 30 is a right-aligned descending triangle. Program 31 alternates digits and stars in a symmetric diamond shape.
Replace 5 with n in both outer loops — see Example 2.
O(n²) for n rows because total printed characters grow quadratically across both halves.
Use sc.hasNextInt() before sc.nextInt() — see Example 2 notes.
Yes — System.out.print(j % 2 == 0 ? "*" : i) compacts the if/else logic.

Did you know?

This pattern prints a top half (1..n) and a bottom half (n-1..1). Each row prints 2*i-1 characters, alternating the row number and * using j % 2.

Next: Increasing Triangle from 11

Move on to the increasing triangle starting from 11 in the Java number-pattern series.

Program 32 tutorial →

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