Write a Java program to print sandglass star pattern using for loop.
package ShapePrograms2; import java.util.Scanner; public class Sandglass1 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); int i, j, k; System.out.print("Please Enter Sandglass Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Sandglass Star Pattern"); for (i = 0 ; i <= rows - 1; i++ ) { for (j = 0 ; j < i; j++ ) { System.out.print(" "); } for(k = i; k <= rows - 1; k++) { System.out.print("* "); } System.out.println(); } for (i = rows - 1; i >= 0; i-- ) { for (j = 0 ; j < i; j++ ) { System.out.print(" "); } for(k = i; k <= rows - 1; k++) { System.out.print("* "); } System.out.println(); } } }
This Java example displays the stars in a sandglass structure or pattern using a while loop.
package ShapePrograms2; import java.util.Scanner; public class Sandglass2 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Please Enter Sandglass Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Sandglass Star Pattern"); int i = 0, j, k; while (i <= rows - 1) { j = 0 ; while(j < i ) { System.out.print(" "); j++; } k = i; while( k <= rows - 1) { System.out.print("* "); k++; } System.out.println(); i++; } i = rows - 1; while ( i >= 0 ) { j = 0 ; while ( j < i) { System.out.print(" "); j++; } k = i; while(k <= rows - 1) { System.out.print("* "); k++; } System.out.println(); i--; } } }
Please Enter Sandglass Pattern Rows = 8
Printing Sandglass Star Pattern
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
Java program to print sandglass star pattern using do while loop
package ShapePrograms2; import java.util.Scanner; public class Sandglass3 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Please Enter Sandglass Pattern Rows = "); int rows = sc.nextInt(); System.out.println("Printing Sandglass Star Pattern"); int i = 0, j, k; do { j = 0 ; do { System.out.print(" "); } while(j++ < i ); k = i; do { System.out.print("* "); } while( ++k <= rows - 1); System.out.println(); } while (++i <= rows - 1); i = rows - 1; do { j = 0 ; do { System.out.print(" "); } while ( j++ < i); k = i; do { System.out.print("* "); } while(++k <= rows - 1); System.out.println(); } while ( --i >= 0 ); } }
Please Enter Sandglass Pattern Rows = 9
Printing Sandglass Star Pattern
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
In this Java example, the SandglassPattern function prints a given symbol’s sandglass pattern.
package ShapePrograms2; import java.util.Scanner; public class Sandglass4 { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System.in); System.out.print("Please Enter Sandglass Pattern Rows = "); int rows = sc.nextInt(); System.out.print("Enter Character for Sandglass Pattern = "); char ch = sc.next().charAt(0); System.out.println("Printing Sandglass Star Pattern"); SandglassPattern(rows, ch); } public static void SandglassPattern(int rows, char ch) { int i, j, k; for (i = 0 ; i <= rows - 1; i++ ) { for (j = 0 ; j < i; j++ ) { System.out.print(" "); } for(k = i; k <= rows - 1; k++) { System.out.print(ch + " "); } System.out.println(); } for (i = rows - 1; i >= 0; i-- ) { for (j = 0 ; j < i; j++ ) { System.out.print(" "); } for(k = i; k <= rows - 1; k++) { System.out.print(ch + " "); } System.out.println(); } } }
Please Enter Sandglass Pattern Rows = 10
Enter Character for Sandglass Pattern = @
Printing Sandglass Star Pattern
@ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @ @ @ @
@ @ @ @ @ @
@ @ @ @ @
@ @ @ @
@ @ @
@ @
@
@
@ @
@ @ @
@ @ @ @
@ @ @ @ @
@ @ @ @ @ @
@ @ @ @ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @