Java Program to Print Hollow Sandglass Star Pattern

Program to Print Hollow Sandglass Star Pattern

In this article we are going to see how to print Hollow sand glass  star program.

Example-1

When row value=4
* * * *
 *   *
  * *
   *
  * *
 *   *
* * * *
Example-2:

When row value=5
* * * * *
 *     *
  *   *
   * *
    *
   * *
  *   *
 *     *
* * * * *

Now, let’s see the actual program how to print it.

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Approach:

  • Enter total row and store it in an integer variable row.
  • Take first for loop to print upper hollow part .
    • Take first inner for loop to print spaces .
    • Take second inner for loop to print stars according to the condition
      if(r == 1 || c == r|| c == row )
  • Take first for loop to print upper hollow part .
    • Take first inner for loop to print spaces .
    • Take second inner for loop to print stars according to the condition
      if(r == 1 || c == r || c == row)
  • Then go on printing the star symbol according to loop

JAVA Code:

Method-1 : Static Star Character

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   // taking variable for loop iteration and row .
    int row,r,c,d;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    //outer for loop 
    // this for loop is responsible to print upper hollow part.
      for (r = 1; r<= row ; r++)
        {
            for (c = 1; c < r; c++)
                System.out.print(" ");
            for (c = r; c <= row ; c++)
            {
                if(r == 1 || c == r|| c == row )
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }
      // this for loop is responsible to print lower hollow part.
        for (r = row -1; r >= 1;r--)
        {
            for (c = 1; c < r; c++)
                System.out.print(" ");
            for (c = r; c <= row; c++)
            {
                if(r == 1 || c == r || c == row)
                    System.out.print("* ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }
    }
}

Output :

Enter row :  5 
* * * * *
 *     *
  *   *
   * *
    *
   * *
  *   *
 *     *
* * * * *

Method-2 : User Input Character

import java.util.*;
public class Main 
{    
    public static void main(String args[])   
    {   
    // taking variable for loop iteration and row .
    int row,r,c,d;
    char ch;
    //creating object 
    Scanner s = new Scanner(System.in);
    // entering the number of row
    System.out.print("Enter rows : ");
    row = s.nextInt();
    // entering any random character
    System.out.print("Enter character : ");
    ch = s.next().charAt(0);
    //outer for loop
     // this for loop is responsible to print upper hollow part. 
      for (r = 1; r<= row ; r++)
        {
            for (c = 1; c < r; c++)
                System.out.print(" ");
            for (c = r; c <= row ; c++)
            {
                if(r == 1 || c == r|| c == row )
                    System.out.print(ch+" ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }
        // this for loop is responsible to print lower hollow part.
        for (r = row -1; r >= 1;r--)
        {
            for (c = 1; c < r; c++)
                System.out.print(" ");
            for (c = r; c <= row; c++)
            {
                if(r == 1 || c == r || c == row)
                    System.out.print(ch+" ");
                else
                    System.out.print("  ");
            }
            System.out.println();
        }
    }
}

Output :

Enter row :  5 
Enter character : *
* * * * *
 *     *
  *   *
   * *
    *
   * *
  *   *
 *     *
* * * * *

C Code:

#include <stdio.h>
int main() {
   int r, row, c ,d;
   printf("Enter rows: ");
   scanf("%d", &row);
      for (r = 1; r<= row ; r++)
        {
            for (c = 1; c < r; c++)
                printf(" ");
            for (c = r; c <= row ; c++)
            {
                if(r == 1 || c == r|| c == row )
                    printf("* ");
                else
                    printf("  ");
            }
            printf("\n");
        }
        for (r = row -1; r >= 1;r--)
        {
            for (c = 1; c < r; c++)
                printf(" ");
            for (c = r; c <= row; c++)
            {
                if(r == 1 || c == r || c == row)
                   printf("* ");
                else
                    printf("  ");
            }
            printf("\n");
        }
   return 0;
}

Output :

Enter row :  5 
* * * * *
 *     *
  *   *
   * *
    *
   * *
  *   *
 *     *
* * * * *

C++ Code:

#include <iostream>
using namespace std;
int main()
{
   int row, r , c ,d ;
   cout << "Enter  rows: ";
   cin >> row;
      for (r = 1; r<= row ; r++)
        {
            for (c = 1; c < r; c++)
                cout << " ";
            for (c = r; c <= row ; c++)
            {
                if(r == 1 || c == r|| c == row )
                   cout <<"* ";
                else
                    cout <<"  ";
            }
           cout <<"\n";
        }
        for (r = row -1; r >= 1;r--)
        {
            for (c = 1; c < r; c++)
                cout <<" ";
            for (c = r; c <= row; c++)
            {
                if(r == 1 || c == r || c == row)
                    cout <<"* ";
                else
                   cout <<"  ";
            }
            cout << "\n";
        }
   return 0;
}

Output :

Enter row :  5 
* * * * *
 *     *
  *   *
   * *
    *
   * *
  *   *
 *     *
* * * * *

Related Java Star Pattern Programs: