Write a Java Program to print half diamond star pattern or the right-side half diamond star pattern using for loop. This example prints the right and inverted right triangles because the combination of both gives the right-side half of the diamond.
package ShapePrograms; import java.util.Scanner; public class HalfDiamondPattern1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i, j; System.out.print("Enter Half Diamond Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Half Diamond Star Pattern"); for (i = 1 ; i <= rows; i++ ) { for (j = 1 ; j <= i; j++ ) { System.out.print("*"); } System.out.println(); } for (i = rows - 1 ; i > 0; i-- ) { for (j = 1 ; j <= i; j++ ) { System.out.print("*"); } System.out.println(); } } }
In this Java Half Diamond star pattern program, we replaced the for loop with a while loop.
package ShapePrograms; import java.util.Scanner; public class HalfDiamondPattern2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i, j; System.out.print("Enter Half Diamond Pattern Rows = "); int rows = sc.nextInt(); i = 1 ; while ( i <= rows ) { j = 1 ; while (j <= i ) { System.out.print("*"); j++; } System.out.println(); i++; } i = rows - 1; while ( i > 0) { j = 1 ; while ( j <= i ) { System.out.print("*"); j++; } System.out.println(); i-- ; } } }
Enter Half Diamond Pattern Rows = 7
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
Java Program to Print Half Diamond Star Pattern using do while loop
package ShapePrograms; import java.util.Scanner; public class HalfDiamondPattern3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i, j; System.out.print("Enter Half Diamond Pattern Rows = "); int rows = sc.nextInt(); i = 1 ; do { j = 1 ; do { System.out.print("*"); } while (++j <= i ) ; System.out.println(); } while ( ++i <= rows ) ; i = rows - 1; do { j = 1 ; do { System.out.print("*"); } while ( ++j <= i ) ; System.out.println(); } while ( --i > 0) ; } }
Enter Half Diamond Pattern Rows = 9
*
**
***
****
*****
******
*******
********
*********
********
*******
******
*****
****
***
**
*
In this example, the HalfDiamondPattern function prints the half diamond pattern of a given symbol.
package ShapePrograms; import java.util.Scanner; public class HalfDiamondPattern4 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Enter Half Diamond Pattern Rows = "); int rows = sc.nextInt(); System.out.print("Enter Character for Hollow Diamond Pattern = "); char ch = sc.next().charAt(0); HalfDiamondPattern(rows, ch); } public static void HalfDiamondPattern(int rows, char ch) { int i, j; for (i = 1 ; i <= rows; i++ ) { for (j = 1 ; j <= i; j++ ) { System.out.print(ch); } System.out.println(); } for (i = rows - 1 ; i > 0; i-- ) { for (j = 1 ; j <= i; j++ ) { System.out.print(ch); } System.out.println(); } } }
Enter Half Diamond Pattern Rows = 12
Enter Character for Hollow Diamond Pattern = #
#
##
###
####
#####
######
#######
########
#########
##########
###########
############
###########
##########
#########
########
#######
######
#####
####
###
##
#