Java Program to Print Hollow Sandglass Star Pattern

Write a Java program to print hollow sandglass star pattern using for loop.

package Shapes3;

import java.util.Scanner;

public class HollowSandglassStar1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j, k;
		
		System.out.print("Enter Hollow Sandglass Star Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Hollow Sandglass Star Pattern");
		
		for (i = 1; i <= rows; i++ ) 
		{
			for (j = 1; j < i; j++ ) 
			{
				System.out.print(" ");
			}
			for(k = i; k <= rows; k++) 
			{
				if(i == 1 || k == i || k == rows)
				{
					System.out.print("* ");
				}
				else 
				{
					System.out.print("  ");
				}
			}
			System.out.println();
		}
		
		for (i = rows - 1; i >= 1; i-- ) 
		{
			for (j = 1; j < i; j++ ) 
			{
				System.out.print(" ");
			}
			for(k = i; k <= rows; k++) 
			{
				if(i == 1 || k == i || k == rows)
				{
					System.out.print("* ");
				}
				else 
				{
					System.out.print("  ");
				}
			}
			System.out.println();
		}
	}
}
Java Program to Print Hollow Sandglass Star Pattern

This Java program displays the hollow sandglass pattern of stars using a while loop.

package Shapes3;

import java.util.Scanner;

public class HollowSandglassStar2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j, k;
		
		System.out.print("Enter Hollow Sandglass Star Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Hollow Sandglass Star Pattern");
		i = 1;
		while( i <= rows ) 
		{
			j = 1;
			while( j < i ) 
			{
				System.out.print(" ");
				j++;
			}
			k = i;
			while( k <= rows) 
			{
				if(i == 1 || k == i || k == rows)
				{
					System.out.print("* ");
				}
				else 
				{
					System.out.print("  ");
				}
				k++;
			}
			System.out.println();
			i++;
		}
		
		i = rows - 1;
		while( i >= 1 ) 
		{
			j = 1;
			while( j < i ) 
			{
				System.out.print(" ");
				j++;
			}
			k = i;
			while( k <= rows) 
			{
				if(i == 1 || k == i || k == rows)
				{
					System.out.print("* ");
				}
				else 
				{
					System.out.print("  ");
				}
				k++;
			}
			System.out.println();
			i--;
		}
	}
}
Enter Hollow Sandglass Star Pattern Rows = 7
Printing Hollow Sandglass Star Pattern
* * * * * * * 
 *         * 
  *       * 
   *     * 
    *   * 
     * * 
      * 
     * * 
    *   * 
   *     * 
  *       * 
 *         * 
* * * * * * * 

This Java Program uses the do while loop to print the hollow sandglass pattern of stars.

package Shapes3;

import java.util.Scanner;

public class HollowSandglassStar3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		int i, j, k;
		
		System.out.print("Enter Hollow Sandglass Star Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.println("Printing Hollow Sandglass Star Pattern");
		i = 1;
		do 
		{
			j = 1;
			do
			{
				System.out.print(" ");
			} while( j++ <= i );
			k = i;
			do 
			{
				if(i == 1 || k == i || k == rows)
				{
					System.out.print("* ");
				}
				else 
				{
					System.out.print("  ");
				}
			} while( ++k <= rows);
			System.out.println();
		} while(++i <= rows );
		
		i = rows - 1;
		do 
		{
			j = 1;
			do 
			{
				System.out.print(" ");
			} while( j++ <= i );
			k = i;
			do 
			{
				if(i == 1 || k == i || k == rows)
				{
					System.out.print("* ");
				}
				else 
				{
					System.out.print("  ");
				}
			} while( ++k <= rows);
			System.out.println();
		} while(--i >= 1 );
	}
}
Enter Hollow Sandglass Star Pattern Rows = 11
Printing Hollow Sandglass Star Pattern
  * * * * * * * * * * * 
   *                 * 
    *               * 
     *             * 
      *           * 
       *         * 
        *       * 
         *     * 
          *   * 
           * * 
            * 
           * * 
          *   * 
         *     * 
        *       * 
       *         * 
      *           * 
     *             * 
    *               * 
   *                 * 
  * * * * * * * * * * *

In this Java example, HollowSandglassPattern function allows user to enter the character and prints the hollow sandglass pattern of the given character.

package Shapes3;

import java.util.Scanner;

public class HollowSandglassStar4 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Enter Hollow Sandglass Star Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.print("Enter Character for Hollow Sandglass Pattern = ");
		char ch = sc.next().charAt(0);
		
		System.out.println("Printing Hollow Sandglass Star Pattern");
		HollowSandglassPattern(rows, ch);
		
	}
	public static void HollowSandglassPattern(int rows, char ch) 
	{
		int i, j, k;
		
		for (i = 1; i <= rows; i++ ) 
		{
			for (j = 1; j < i; j++ ) 
			{
				System.out.print(" ");
			}
			for(k = i; k <= rows; k++) 
			{
				if(i == 1 || k == i || k == rows)
				{
					System.out.print(ch + " ");
				}
				else 
				{
					System.out.print("  ");
				}
			}
			System.out.println();
		}
		
		for (i = rows - 1; i >= 1; i-- ) 
		{
			for (j = 1; j < i; j++ ) 
			{
				System.out.print(" ");
			}
			for(k = i; k <= rows; k++) 
			{
				if(i == 1 || k == i || k == rows)
				{
					System.out.print(ch + " ");
				}
				else 
				{
					System.out.print("  ");
				}
			}
			System.out.println();
		}
	}
}
Enter Hollow Sandglass Star Pattern Rows = 15
Enter Character for Hollow Sandglass Pattern = &
Printing Hollow Sandglass Star Pattern
& & & & & & & & & & & & & & & 
 &                         & 
  &                       & 
   &                     & 
    &                   & 
     &                 & 
      &               & 
       &             & 
        &           & 
         &         & 
          &       & 
           &     & 
            &   & 
             & & 
              & 
             & & 
            &   & 
           &     & 
          &       & 
         &         & 
        &           & 
       &             & 
      &               & 
     &                 & 
    &                   & 
   &                     & 
  &                       & 
 &                         & 
& & & & & & & & & & & & & & & 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.