Write a Java Program to print hollow inverted star pyramid pattern using for loop. This Java inverted pyramid pattern example uses if condition inside a nested for loops to identify the edges and display the hollow inverted pyramid.
package ShapePrograms; import java.util.Scanner; public class HollowInvertedPyramid1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Hollow Inverted Pyramid Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Hollow Inverted Pyramid Star Pattern"); for (int i = rows ; i > 0; i-- ) { for (int j = 1 ; j <= rows - i; j++ ) { System.out.print(" "); } for (int k = 1 ; k <= (2 * i) - 1; k++ ) { if(i == 1 || i == rows || k == 1 || k == (2 * i) - 1) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } } }
In this Java hollow inverted star pyramid program, we replaced the for loops with while loops.
package ShapePrograms; import java.util.Scanner; public class HollowInvertedPyramid2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Hollow Inverted Pyramid Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Hollow Inverted Pyramid Star Pattern"); int i = rows, j, k; while( i > 0) { j = 1 ; while(j <= rows - i ) { System.out.print(" "); j++; } k = 1 ; while(k <= (2 * i) - 1 ) { if(i == 1 || i == rows || k == 1 || k == (2 * i) - 1) { System.out.print("*"); } else { System.out.print(" "); } k++; } System.out.println(); i-- ; } } }
Enter Hollow Inverted Pyramid Pattern Rows = 9
Hollow Inverted Pyramid Star Pattern
*****************
* *
* *
* *
* *
* *
* *
* *
*
Java Program to Print Hollow Inverted Star Pyramid using do while loop
package ShapePrograms; import java.util.Scanner; public class HollowInvertedPyramid3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Hollow Inverted Pyramid Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Hollow Inverted Pyramid Star Pattern"); int i = rows, j, k; do { j = 1 ; do { System.out.print(" "); } while(j++ <= rows - i ) ; k = 1 ; do { if(i == 1 || i == rows || k == 1 || k == (2 * i) - 1) { System.out.print("*"); } else { System.out.print(" "); } } while(++k <= (2 * i) - 1 ) ; System.out.println(); } while( --i > 0) ; } }
Enter Hollow Inverted Pyramid Pattern Rows = 13
Hollow Inverted Pyramid Star Pattern
*************************
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
In this Java example, the HollowInvertedPyramidPat function prints the hollow inverted pyramid pattern of a given symbol.
package ShapePrograms; import java.util.Scanner; public class HollowInvertedPyramid4 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Hollow Inverted Pyramid Pattern Rows = "); int rows = sc.nextInt(); System.out.print("Enter Character for Exponentially Increased Pattern = "); char ch = sc.next().charAt(0); System.out.println("Hollow Inverted Pyramid Star Pattern"); HollowInvertedPyramidPat(rows, ch); } public static void HollowInvertedPyramidPat(int rows, char ch) { for (int i = rows ; i > 0; i-- ) { for (int j = 1 ; j <= rows - i; j++ ) { System.out.print(" "); } for (int k = 1 ; k <= (2 * i) - 1; k++ ) { if(i == 1 || i == rows || k == 1 || k == (2 * i) - 1) { System.out.print(ch); } else { System.out.print(" "); } } System.out.println(); } } }
Enter Hollow Inverted Pyramid Pattern Rows = 17
Enter Character for Exponentially Increased Pattern = #
Hollow Inverted Pyramid Star Pattern
#################################
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
#