Java Program to Print Inverted Pyramid Pattern
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
How to Think About It
Algorithm
Code
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(); } } }
Dry Run
Let's trace the program with n=5 to see how it prints the inverted pyramid.
Start outer loop with i=5
Print 0 spaces, then print 9 stars (2*5-1).
Next outer loop i=4
Print 1 space, then print 7 stars (2*4-1).
Next outer loop i=3
Print 2 spaces, then print 5 stars (2*3-1).
Next outer loop i=2
Print 3 spaces, then print 3 stars (2*2-1).
Last outer loop i=1
Print 4 spaces, then print 1 star (2*1-1).
| i (row) | Spaces printed | Stars printed |
|---|---|---|
| 5 | 0 | 9 |
| 4 | 1 | 7 |
| 3 | 2 | 5 |
| 2 | 3 | 3 |
| 1 | 4 | 1 |
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
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--; } } }
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); } }
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For loops | O(n^2) | O(1) | Simple and clear code |
| While loops | O(n^2) | O(1) | Beginners preferring while syntax |
| Recursion | O(n^2) | O(n) | Elegant but uses call stack |