Write a Java Program to print hollow pyramid star pattern using for loop. This pattern example uses two if-else statements and two for loops nested inside another to iterate and display a hollow pyramid.
package ShapePrograms; import java.util.Scanner; public class HollowPyramidPattern1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Please Enter Hollow Pyramid Pattern Rows = "); int rows = sc.nextInt(); System.out.println("---- Printing Hollow Pyramid Pattern of Stars ----"); int i, j, k; for (i = 1 ; i <= rows; i++ ) { for (j = 1 ; j <= rows - i; j++ ) { System.out.print(" "); } if(i == 1 || i == rows) { for (k = 1 ; k <= (i * 2) - 1; k++ ) { System.out.print("*"); } } else { for (k = 1; k <= (i * 2) - 1; k++ ) { if(k == 1 || k == i * 2 - 1) { System.out.print("*"); } else { System.out.print(" "); } } } System.out.println(); } } }
Please Enter Hollow Pyramid Pattern Rows = 8
---- Printing Hollow Pyramid Pattern of Stars ----
*
* *
* *
* *
* *
* *
* *
***************
We simplified the above code and removed the extra if-else to display the hollow pyramid star pattern.
package ShapePrograms; import java.util.Scanner; public class HollowPyramidPattern2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Please Enter Hollow Pyramid Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Hollow Pyramid Pattern of Stars"); int i, j, k; for (i = 1 ; i <= rows; i++ ) { for (j = 1 ; j <= rows - i; j++ ) { System.out.print(" "); } for (k = 1; k <= (i * 2) - 1; k++ ) { if(k == 1 || k == i * 2 - 1 || i == rows) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); } } }
In this Java hollow pyramid star pattern program, we replaced the for loops with while loops.
package ShapePrograms; import java.util.Scanner; public class HollowPyramidPattern3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Please Enter Hollow Pyramid Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Hollow Pyramid Pattern of Stars"); int i = 1, j, k; while( i <= rows) { j = 1 ; while ( j <= rows - i ) { System.out.print(" "); j++; } k = 1; while ( k <= (i * 2) - 1) { if(k == 1 || k == i * 2 - 1 || i == rows) { System.out.print("*"); } else { System.out.print(" "); } k++ ; } System.out.println(); i++; } } }
Please Enter Hollow Pyramid Pattern Rows = 10
Printing Hollow Pyramid Pattern of Stars
*
* *
* *
* *
* *
* *
* *
* *
* *
*******************
Java Program to Print Hollow Pyramid Star Pattern using do while loop
package ShapePrograms; import java.util.Scanner; public class HollowPyramidPattern4 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Please Enter Hollow Pyramid Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Hollow Pyramid Pattern of Stars"); int i = 1, j, k; do { j = 1 ; do { System.out.print(" "); } while (++j <= rows - i + 1); k = 1; do { if(k == 1 || k == i * 2 - 1 || i == rows) { System.out.print("*"); } else { System.out.print(" "); } } while ( ++k <= (i * 2) - 1); System.out.println(); } while( ++i <= rows); } }
Please Enter Hollow Pyramid Pattern Rows = 13
Printing Hollow Pyramid Pattern of Stars
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
*************************
In this example, the HollowMirroredRhombusPat function prints the hollow pyramid pattern of a given symbol.
package ShapePrograms; import java.util.Scanner; public class HollowPyramidPattern5 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Please Enter Hollow Pyramid Pattern Rows = "); int rows = sc.nextInt(); System.out.print("Enter Character for Hollow Pyramid Pattern = "); char ch = sc.next().charAt(0); System.out.println("---- Printing Hollow Pyramid Pattern ----"); HollowPyramidPatternPat(rows, ch); } public static void HollowPyramidPatternPat(int rows, char ch) { int i, j, k; for (i = 1 ; i <= rows; i++ ) { for (j = 1 ; j <= rows - i; j++ ) { System.out.print(" "); } for (k = 1; k <= (i * 2) - 1; k++ ) { if(k == 1 || k == i * 2 - 1 || i == rows) { System.out.print(ch); } else { System.out.print(" "); } } System.out.println(); } } }
Please Enter Hollow Pyramid Pattern Rows = 15
Enter Character for Hollow Pyramid Pattern = #
---- Printing Hollow Pyramid Pattern ----
#
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
# #
#############################