Inverted Center-Aligned Pyramid Star Pattern in Java

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

What Is This Pattern?

An inverted center-aligned pyramid prints the widest odd-width star row first, then narrows to a single tip star — with leading spaces so each shorter row stays centered.

Remember
Rule: for i from rows down to 1,
      print (rows - i) spaces, then (2 * i - 1) stars

*********
 *******
  *****
   ***
    *     ← 5 rows (base on top)

It is the flip of Program 5: keep the same space and star formulas, reverse only the outer loop. That mirrors how Program 2 inverts Program 1, but with odd-width centering.

How to Solve It

Two ways to emit the same shape — start with nested loops, then optionally shorten with String.repeat.

MethodIdeaBest for
Nested loopsCountdown i; spaces then odd stars via printLearning, interviews, exams
String.repeatBuild margin and star run in one call eachShorter demos once formulas click (Java 11+)

Pseudocode

Pseudocode
for i from rows down to 1:
    print (rows - i) spaces (no newline)
    print (2 * i - 1) stars (no newline)
    print newline

Cheat sheet

GoalPattern
Countdown rowsfor (i = rows; i >= 1; i--)
Leading spacesfor (j = 1; j <= rows - i; j++) System.out.print(" ");
Odd star runfor (k = 1; k <= 2 * i - 1; k++) System.out.print("*");
End the rowSystem.out.println();
First printed row0 spaces + 2 * rows - 1 stars
One-line shortcutSystem.out.print(" ".repeat(rows - i)); System.out.println("*".repeat(2 * i - 1));
Upright versionfor (i = 1; i <= rows; i++) → Program 5

print vs println

APIEffectUse for
System.out.printStays on the same lineEach space and each *
System.out.printlnEnds the current lineAfter both inner loops

Live Preview

Change the height and the inverted pyramid updates instantly — including the star total (n²).

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

Live result 5 rows · 25 stars
*********
 *******
  *****
   ***
    *

Worked Walkthrough — rows = 4

Trace spaces, stars, and the printed line as i counts down from 4 to 1.

iSpaces rows - iStars 2*i - 1Printed row
407*******
315*****
223***
131*

Star total: 7 + 5 + 3 + 1 = 16 = 4² — same as Program 5, only the print order differs. That square sum is why time is O(n²).

Java Programs

Three complete programs: fixed rows, Scanner input, and a String.repeat shortcut. Use View Output to reveal sample results.

Example 1 — Fixed rows = 5

Hard-coded height — countdown outer loop with space and star inner loops.

Java
public class InvertedPyramid {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = rows; i >= 1; i--) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

How It Works

1. Set height. rows = 5 means five lines from base to tip.

2. Outer loop counts down. i runs from rows down to 1 — widest row first.

3. Spaces, then stars. Print rows - i spaces, then 2 * i - 1 stars with System.out.print.

4. Break the line. System.out.println() after both inner loops starts the next row.

When i = 5 you get 0 spaces and 9 stars; when i = 1 you get 4 spaces and 1 star.

Example 2 — User Input Version

Read the height at runtime with Scanner. Prefer hasNextInt() in real apps (shown in the tip below).

Java
import java.util.Scanner;

public class InvertedPyramidInput {
    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 = rows; i >= 1; i--) {
            for (int j = 1; j <= rows - i; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

How It Works

1. Prompt and read. Ask for a row count, then store it with sc.nextInt().

2. Same countdown core. Only the source of rows changes — the space and star logic matches Example 1.

3. Safer input tip. Non-numeric input throws InputMismatchException. Prefer:

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

Example 3 — "*".repeat() for Spaces and Stars

Build each margin and odd star run in one call — same shape, no explicit character loops (Java 11+).

Java
public class InvertedPyramidRepeat {
    public static void main(String[] args) {
        int rows = 5;

        for (int i = rows; i >= 1; i--) {
            System.out.print(" ".repeat(rows - i));
            System.out.println("*".repeat(2 * i - 1));
        }
    }
}

How It Works

1. Same countdown. Still walk i from rows down to 1.

2. Build each segment. " ".repeat(rows - i) is the margin; "*".repeat(2 * i - 1) is the star run.

3. Print and advance. Use print for spaces and println for stars so the row ends correctly.

Learn the two-loop version first (Examples 1–2) so you can explain both bounds in an interview; treat this as a polish shortcut afterward.

Edge Cases & Pitfalls

Check these before calling the solution done.

i++

Upright pyramid by mistake

If you increment i from 1 to rows, you reprint Program 5. Use i-- from rows down to 1.

Tabs

Broken centering

Always print the space character " ", not tabs — tab width varies and skews the tip.

println inside

Column of stars

If println is inside either inner loop, each character lands on its own line. Use print for spaces and stars; println only after both loops.

rows = 1

Single tip star

Output is just * — base and tip coincide. A good sanity check.

rows ≤ 0

Empty output

Outer loop never runs. Validate and re-prompt for interactive programs.

Bad input

Use hasNextInt()

nextInt() throws on letters — prefer sc.hasNextInt() and require rows >= 1.

Time and Space Complexity

ProgramTimeExtra space
Nested loops (Examples 1–2)O(rows²)O(1)
String.repeat (Example 3)O(rows²)O(rows) temporary per row string

Total stars = 1 + 3 + … + (2n - 1) = n², plus up to Θ(n) spaces per row — still quadratic in n. Same totals as Program 5.

Key Takeaways

  • Rule: countdown i; print rows - i spaces then 2 * i - 1 stars.
  • Flip of Program 5: same formulas — only reverse the outer loop.
  • Break the row: call println only after both space and star loops.
  • Complexity: O(n²) time from n² stars; O(1) extra space for nested loops.

One line: for i from rows down to 1, print rows - i spaces and 2 * i - 1 stars, then println.

Frequently Asked Questions

The outer loop runs i from rows down to 1. Stars still use 2*i-1, so large i prints many stars first. As i shrinks, stars become 9,7,5,… and spaces (rows-i) grow from 0 upward. Same formulas as Program 5 — only the order of i changes.
Spaces use (rows-i). When i is rows, the margin is 0; when i is 1, the margin is rows-1. As i steps down, the margin grows while 2*i-1 shrinks, which keeps shorter rows centered under the wide top.
Program 5 uses for (i = 1; i <= rows; i++) so stars grow. Program 6 uses for (i = rows; i >= 1; i--) with the same inner loops, so the base prints first and the tip last.
Program 2 is an inverted left-aligned triangle (i stars, no centering). Program 6 keeps (rows-i) spaces and odd star runs so the tip stays centered.
System.out.print stays on the same line. System.out.println ends the current line. Spaces and stars use print; the row break uses println after both inner loops.
O(n²) for n rows. Same totals as Program 5; only iteration order differs. Total stars equal n².
Yes. With the countdown outer loop: System.out.print(" ".repeat(rows - i)); System.out.println("*".repeat(2 * i - 1)); (Java 11+).
Check sc.hasNextInt() before sc.nextInt() so bad input does not throw InputMismatchException.

Did you know?

This inverted pyramid is exactly Program 5 with the outer loop reversed — same formulas, different print order. Total stars still equal n².

Next: Inverted V Hollow

Move from a filled inverted pyramid to a hollow inverted-V outline.

Program 7 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