0
0
JavaProgramBeginner · 2 min read

Java Program to Print Inverted Pyramid Pattern

You can print an inverted pyramid in Java using nested for loops where the outer loop controls the rows and the inner loops print spaces and stars; for example, for (int i = n; i >= 1; i--) { for (int j = 1; j <= n - i; j++) System.out.print(" "); for (int k = 1; k <= 2 * i - 1; k++) System.out.print("*"); System.out.println(); } prints an inverted pyramid of height n.
📋

Examples

Input5
Output********* ******* ***** *** *
Input3
Output***** *** *
Input1
Output*
🧠

How to Think About It

To print an inverted pyramid, think of printing rows from the largest width to the smallest. Each row has some spaces on the left to center the stars, and then a decreasing number of stars. Use one loop to handle rows from the top to bottom, and inside it, print spaces first, then stars.
📐

Algorithm

1
Get the number of rows (height) for the pyramid.
2
For each row from the top (largest) to bottom (smallest):
3
Print spaces equal to the number of rows minus the current row number to shift stars right.
4
Print stars in decreasing odd numbers starting from (2 * height - 1).
5
Move to the next line after printing each row.
💻

Code

java
public class InvertedPyramid {
    public static void main(String[] args) {
        int n = 5; // height of the pyramid
        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= n - i; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= 2 * i - 1; k++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}
Output
********* ******* ***** *** *
🔍

Dry Run

Let's trace the program with n=5 to see how it prints the inverted pyramid.

1

Start outer loop with i=5

Print 0 spaces, then print 9 stars (2*5-1).

2

Next outer loop i=4

Print 1 space, then print 7 stars (2*4-1).

3

Next outer loop i=3

Print 2 spaces, then print 5 stars (2*3-1).

4

Next outer loop i=2

Print 3 spaces, then print 3 stars (2*2-1).

5

Last outer loop i=1

Print 4 spaces, then print 1 star (2*1-1).

i (row)Spaces printedStars printed
509
417
325
233
141
💡

Why This Works

Step 1: Outer loop controls rows

The outer for loop runs from n down to 1, controlling how many rows the pyramid has and decreasing the number of stars each row.

Step 2: Print spaces to align stars

The first inner loop prints spaces to shift the stars to the right, making the pyramid shape. The number of spaces increases as the row number decreases.

Step 3: Print stars in odd numbers

The second inner loop prints stars in odd numbers starting from 2*n - 1 and decreasing by 2 each row, forming the inverted pyramid.

🔄

Alternative Approaches

Using while loops
java
public class InvertedPyramid {
    public static void main(String[] args) {
        int n = 5;
        int i = n;
        while (i >= 1) {
            int j = 1;
            while (j <= n - i) {
                System.out.print(" ");
                j++;
            }
            int k = 1;
            while (k <= 2 * i - 1) {
                System.out.print("*");
                k++;
            }
            System.out.println();
            i--;
        }
    }
}
This uses while loops instead of for loops, which some beginners find easier to understand but is more verbose.
Using recursion
java
public class InvertedPyramid {
    public static void printRow(int n, int i) {
        if (i == 0) return;
        for (int j = 0; j < n - i; j++) System.out.print(" ");
        for (int k = 0; k < 2 * i - 1; k++) System.out.print("*");
        System.out.println();
        printRow(n, i - 1);
    }
    public static void main(String[] args) {
        printRow(5, 5);
    }
}
This recursive approach prints each row by calling itself with a smaller row number, which is elegant but less common for beginners.

Complexity: O(n^2) time, O(1) space

Time Complexity

The program uses nested loops: the outer loop runs n times, and the inner loops combined run roughly 2n times per iteration, resulting in O(n^2) time.

Space Complexity

The program uses only a few variables and prints directly, so space complexity is O(1), constant space.

Which Approach is Fastest?

All approaches have similar time complexity; iterative for loops are simplest and fastest in practice, while recursion adds overhead.

ApproachTimeSpaceBest For
For loopsO(n^2)O(1)Simple and clear code
While loopsO(n^2)O(1)Beginners preferring while syntax
RecursionO(n^2)O(n)Elegant but uses call stack
💡
Use nested loops: one for spaces and one for stars to shape the inverted pyramid.
⚠️
Beginners often forget to print the correct number of spaces, causing the pyramid to be misaligned.