Write a Java Program to print mirrored half diamond star pattern or a Program to print the left-side half diamond star pattern using for loop.
package ShapePrograms; import java.util.Scanner; public class MirroredHalfDiamond1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Mirrored Half Diamond Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Mirrored Half Diamond Star Pattern"); 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; k++ ) { System.out.print("*"); } System.out.println(); } for (i = rows - 1 ; i > 0; i-- ) { for (j = 1 ; j <= rows - i; j++ ) { System.out.print(" "); } for (k = 1 ; k <= i; k++ ) { System.out.print("*"); } System.out.println(); } } }
We replaced the for loop with a while loop in this mirrored half diamond star pattern program.
package ShapePrograms; import java.util.Scanner; public class MirroredHalfDiamond2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Diamond Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Diamond Star Pattern"); int i, j, k; i = 1 ; while( i <= rows) { j = 1 ; while(j <= rows - i ) { System.out.print(" "); j++; } k = 1 ; while(k <= i) { System.out.print("*"); k++ ; } System.out.println(); i++ ; } i = rows - 1 ; while( i > 0) { j = 1 ; while( j <= rows - i ) { System.out.print(" "); j++; } k = 1 ; while ( k <= i ) { System.out.print("*"); k++ ; } System.out.println(); i-- ; } } }
Enter Diamond Pattern Rows = 7
Printing Diamond Star Pattern
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
Java Program to Print Mirrored Half Diamond Star Pattern using do while loop
package ShapePrograms; import java.util.Scanner; public class MirroredHalfDiamond3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Diamond Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Diamond Star Pattern"); int i, j, k; i = 1 ; do { j = 1 ; do { System.out.print(" "); } while(j++ <= rows - i ) ; k = 1 ; do { System.out.print("*"); } while(++k <= i) ; System.out.println(); } while( ++i <= rows) ; i = rows - 1 ; do { j = 1 ; do { System.out.print(" "); } while( j++ <= rows - i ) ; k = 1 ; do { System.out.print("*"); } while ( ++k <= i ); System.out.println(); } while( --i > 0) ; } }
Enter Diamond Pattern Rows = 10
Printing Diamond Star Pattern
*
**
***
****
*****
******
*******
********
*********
**********
*********
********
*******
******
*****
****
***
**
*
In this example, the MirroredHalfDiamondPattern function prints the mirrored half diamond pattern of a given symbol.
package ShapePrograms; import java.util.Scanner; public class MirroredHalfDiamond4 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Mirrored Half Diamond Pattern Rows = "); int rows = sc.nextInt(); System.out.print("Enter Character for Mirrored Half Diamond = "); char ch = sc.next().charAt(0); MirroredHalfDiamondPattern(rows, ch); } public static void MirroredHalfDiamondPattern(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; k++ ) { System.out.print(ch); } System.out.println(); } for (i = rows - 1 ; i > 0; i-- ) { for (j = 1 ; j <= rows - i; j++ ) { System.out.print(" "); } for (k = 1 ; k <= i; k++ ) { System.out.print(ch); } System.out.println(); } } }
Enter Mirrored Half Diamond Pattern Rows = 12
Enter Character for Mirrored Half Diamond = $
$
$$
$$$
$$$$
$$$$$
$$$$$$
$$$$$$$
$$$$$$$$
$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$$$
$$$$$$$$$$$$
$$$$$$$$$$$
$$$$$$$$$$
$$$$$$$$$
$$$$$$$$
$$$$$$$
$$$$$$
$$$$$
$$$$
$$$
$$
$