Printing star patterns in Java is a fundamental exercise for loops, recursion, and output formatting. Below are different types of pyramid and star patterns, along with concise explanations.
1. Simple Pyramid Pattern
public class GFG {
public static void PyramidStar(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) System.out.print("* ");
System.out.println();
}
}
public static void main(String[] args) {
PyramidStar(5);
}
}
Output
* * * * * * * * * * * * * * *
Explanation:
- The outer loop iterates through the number of rows (n).
- The inner loop prints stars (* ) for each row corresponding to the current row number.
- System.out.println() moves to the next line after printing stars for a row.
Recursive Approach
class GFG {
public static void printRow(int n) {
if (n == 0) return;
System.out.print("* ");
printRow(n - 1);
}
public static void changeRow(int n) {
if (n == 0) return;
changeRow(n - 1);
printRow(n);
System.out.println();
}
public static void main(String[] args) {
changeRow(5);
}
}
Output
* * * * * * * * * * * * * * *
Explanation:
- printRow(n) prints n stars recursively for a single row.
- changeRow(n) recursively handles the rows, calling printRow(n) after processing previous rows.
- System.out.println() moves to the next line after each row.
2. 180° Rotated / Mirrored Pyramid
public class GFG {
public static void FlippedPyramidStar(int n) {
for (int i = 0; i < n; i++) {
for (int j = 2 * (n - i); j >= 0; j--) System.out.print(" ");
for (int j = 0; j <= i; j++) System.out.print("* ");
System.out.println();
}
}
public static void main(String[] args) {
FlippedPyramidStar(5);
}
}
Output
*
* *
* * *
* * * *
* * * * *
Explanation:
- The first inner loop prints spaces to align the stars to the right.
- The second inner loop prints stars corresponding to the current row number.
- System.out.println() moves to the next line after each row.
3. Triangle Star Pattern
public class GFG {
public static void printTriangle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) System.out.print(" ");
for (int j = 0; j <= i; j++) System.out.print("* ");
System.out.println();
}
}
public static void main(String[] args) {
printTriangle(5);
}
}
Output
* * * * * * * * * * * * * * *
Explanation:
- The first inner loop prints spaces to align stars to the right.
- The second inner loop prints stars corresponding to the current row number.
- System.out.println() moves to the next line after each row.
4. Square Star Pattern
class GFG {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) System.out.print("*");
System.out.println();
}
}
}
Output
***** ***** ***** ***** *****
Explanation:
- The outer loop runs for 5 rows.
- The inner loop prints 5 stars (*) in each row.
- System.out.println() moves the cursor to the next line after completing one row.
5. Inverted Pyramid Pattern
Example 1: This code prints an inverted pyramid pattern with a decreasing number of stars per row.
class GFG {
public static void main(String[] args) {
int n = 5;
for (int i = n; i >= 1; i--) {
for (int j = i; j >= 1; j--) System.out.print("* ");
System.out.println();
}
}
}
Output
* * * * * * * * * * * * * * *
Explanation:
- n defines the total number of rows in the pattern.
- The outer loop runs from n down to 1, reducing the number of stars in each row.
- The inner loop prints i stars (* ) in the current row.
- System.out.println() moves to the next line after each row.
Example 2: This code prints an inverted pyramid star pattern using recursion.
class GFG {
public static void printRow(int n) {
if (n == 0) return;
System.out.print("* ");
printRow(n - 1);
}
public static void changeRow(int n) {
if (n == 0) return;
printRow(n);
System.out.println();
changeRow(n - 1);
}
public static void main(String[] args) {
changeRow(5);
}
}
Output
* * * * * * * * * * * * * * *
Explanation:
- printRow(int n) recursively prints n stars in a single row.
- The base case n == 0 stops the recursion.
- changeRow(int n) controls the number of rows printed.
- It first prints a row with n stars, then moves to the next line.
- The recursive call changeRow(n - 1) reduces the number of stars in each subsequent row.
Complexity Analysis
Pattern Type | Time Complexity | Auxiliary Space |
|---|---|---|
Iterative Pyramid/Triangle | O(n²) | O(1) |
Recursive Pyramid/Triangle | O(n²) | O(n) (stack) |
Mirrored Pyramid | O(n²) | O(1) |
Square/Rectangle Star Pattern | O(n²) | O(1) |
Inverted Pyramid | O(n²) | O(1) or O(n) |