Write a Java Program to print hollow mirrored rhombus star pattern using for loop. This pattern example uses multiple if-else statements and nested for loops to iterate and print the hollow mirrored rhombus.
package ShapePrograms; import java.util.Scanner; public class HollowMirroredRhombus1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Hollow Mirrored Rhombus Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Hollow Mirrored Rhombus Star Pattern"); for (int i = 1 ; i <= rows; i++ ) { for (int j = i ; j > 0; j-- ) { System.out.print(" "); } if (i == 1 || i == rows) { for (int k = 1 ; k <= rows; k++ ) { System.out.print("* "); } } else { for (int k = 1 ; k <= rows; k++ ) { if (k == 1 || k == rows) { System.out.print("* "); } else { System.out.print(" "); } } } System.out.println(); } } }
Enter Hollow Mirrored Rhombus Pattern Rows = 7
Printing Hollow Mirrored Rhombus Star Pattern
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
Although the above approach is correct, we simplified the hollow mirrored rhombus code by removing the extra if-else statements.
package ShapePrograms; import java.util.Scanner; public class HollowMirroredRhombus2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Hollow Mirrored Rhombus Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Hollow Mirrored Rhombus Star Pattern"); for (int i = 1 ; i <= rows; i++ ) { for (int j = 1 ; j < i; j++ ) { System.out.print(" "); } for (int k = 1 ; k <= rows; k++ ) { if (i == 1 || i == rows || k == 1 || k == rows) { System.out.print("* "); } else { System.out.print(" "); } } System.out.println(); } } }
In this Java hollow mirrored rhombus star pattern program, we replaced the for loops with while loops.
package ShapePrograms; import java.util.Scanner; public class HollowMirroredRhombus3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i = 1, j, k; System.out.print("Enter Hollow Mirrored Rhombus Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Hollow Mirrored Rhombus Star Pattern"); while ( i <= rows) { j = 1 ; while (j < i ) { System.out.print(" "); j++; } k = 1 ; while( k <= rows ) { if (i == 1 || i == rows || k == 1 || k == rows) { System.out.print("* "); } else { System.out.print(" "); } k++; } System.out.println(); i++ ; } } }
Enter Hollow Mirrored Rhombus Pattern Rows = 10
Printing Hollow Mirrored Rhombus Star Pattern
* * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * *
Java Program to Print Hollow Mirrored Rhombus Star Pattern using do while loop
package ShapePrograms; import java.util.Scanner; public class HollowMirroredRhombus4 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i = 1, j, k; System.out.print("Enter Hollow Mirrored Rhombus Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Hollow Mirrored Rhombus Star Pattern"); do { j = 1 ; do { System.out.print(" "); } while (j++ < i ); k = 1 ; do { if (i == 1 || i == rows || k == 1 || k == rows) { System.out.print("* "); } else { System.out.print(" "); } } while( ++k <= rows ) ; System.out.println(); } while ( ++i <= rows) ; } }
Enter Hollow Mirrored Rhombus Pattern Rows = 12
Printing Hollow Mirrored Rhombus Star Pattern
* * * * * * * * * * * *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* *
* * * * * * * * * * * *
In this example, the HollowMirroredRhombusPat function prints the hollow mirrored rhombus pattern of a given symbol.
package ShapePrograms; import java.util.Scanner; public class HollowMirroredRhombus5 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Hollow Mirrored Rhombus Pattern Rows = "); int rows = sc.nextInt(); System.out.print("Enter Character for Mirrored Hollow Rhombus = "); char ch = sc.next().charAt(0); System.out.println("Printing Hollow Mirrored Rhombus Star Pattern"); HollowMirroredRhombusPat(rows, ch); } public static void HollowMirroredRhombusPat(int rows, char ch) { for (int i = 1 ; i <= rows; i++ ) { for (int j = 1 ; j < i; j++ ) { System.out.print(" "); } for (int k = 1 ; k <= rows; k++ ) { if (i == 1 || i == rows || k == 1 || k == rows) { System.out.print(ch + " "); } else { System.out.print(" "); } } System.out.println(); } } }
Enter Hollow Mirrored Rhombus Pattern Rows = 16
Enter Character for Mirrored Hollow Rhombus = $
Printing Hollow Mirrored Rhombus Star Pattern
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $