BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Print Sandglass Star Pattern

By Chaitanya Singh | Filed Under: Java Examples

In this tutorial, you will learn how to write a java program to print Sandglass star pattern.

Sandglass star Pattern Illustration:

Number of rows: 5
Output:
* * * * *
 * * * *
  * * *
   * *
    *
   * *
  * * *
 * * * *
* * * * *

Example 1: Program to print Sandglass star Pattern

public class JavaExample
{
  public static void main(String[] args)
  {
    //Initialized the number of rows, this represents
    //the number of rows in first half and second half
    //of the Sandglass pattern
    int numberOfRows = 6;

    //Outer loop 1: Prints the first half of the pattern
    for (int i= 0; i<= numberOfRows-1 ; i++)
    {
      //Prints the spaces before the first star of each row
      for (int j=0; j<i; j++)
      {
        System.out.print(" ");
      }
      //Prints stars and the whitespaces in between them
      for (int k=i; k<=numberOfRows-1; k++)
      {
        System.out.print("*" + " ");
      }
      //Next line for new row
      System.out.println();
    }
    //Outer loop 2: Prints the second half of the pattern
    for (int i= numberOfRows-1; i>= 0; i--)
    {
      //Prints the spaces before the first star of each row
      //of the second half of the pattern
      for (int j=0; j<i; j++)
      {
        System.out.print(" ");
      }
      //Prints stars and the whitespaces in between them
      for (int k=i; k<=numberOfRows-1; k++)
      {
        System.out.print("*" + " ");
      }
      //Move the cursor to new line for next row
      System.out.println();
    }
  }
}  

Output:

Print Sandglass pattern in Java

Example 2: Print Sandglass star pattern based on user Input

This program is same as the program shown in first example, except that here the number of rows is not hardcoded and is entered by user during runtime.

import java.util.Scanner;
public class JavaExample
{
  public static void main(String[] args)
  {
    //This represents the number of rows in first half a
    // and second half of the Sand glass pattern
    int numberOfRows;

    //Getting the number of rows from user
    System.out.println("Enter the number of rows: ");
    Scanner sc = new Scanner(System.in);
    numberOfRows = sc.nextInt();
    sc.close();

    //Outer loop 1: Prints the first half of the pattern
    for (int i= 0; i<= numberOfRows-1 ; i++)
    {
      //Inner loop 1: Prints the spaces before the first star of each row
      for (int j=0; j<i; j++)
      {
        System.out.print(" ");
      }
      //Inner loop 2: Prints stars and the whitespaces in between them
      for (int k=i; k<=numberOfRows-1; k++)
      {
        System.out.print("*" + " ");
      }
      //Next line for new row
      System.out.println();
    }
    //Outer loop 2: Prints the second half of the pattern
    for (int i= numberOfRows-1; i>= 0; i--)
    {
      //Inner loop 1: Prints spaces before first star of the row
      for (int j=0; j<i; j++)
      {
        System.out.print(" ");
      }
      //Inner loop 2: Prints stars and the whitespaces in between them
      for (int k=i; k<=numberOfRows-1; k++)
      {
        System.out.print("*" + " ");
      }
      //Move the cursor to new line for next row
      System.out.println();
    }
  }
}  

Output:

Sandglass star pattern in java

Related Java Programs

  1. Java program to print left pascal triangle pattern
  2. Java program to print right pascal triangle pattern
  3. Java program to print right down mirror star pattern
  4. Java program to print reverse pyramid star pattern
❮ Java TutorialJava Programs ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap