Java Program to Print Hourglass Pattern

1. Introduction

This Java program is designed to print a distinctive hourglass pattern composed of asterisks. The pattern consists of a top and bottom filled row, with progressively decreasing and then increasing indents and gaps to create an hourglass silhouette.

2. Program Steps

1. Take the number of rows as input from the user.

2. Use nested loops to print the upper half of the diamond pattern.

3. Use nested loops to print the lower half of the diamond pattern.

4. In both upper and lower halves, use conditional statements to determine when to print an asterisk or a space.

3. Code Program

import java.util.Scanner;

public class StarPatternProgram
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        // Taking rows value from the user
        System.out.println("How many rows you want in this pattern?");
        int rows = sc.nextInt();
        System.out.println("Here is your pattern....!!!");

        // Printing upper half of the pattern
        for (int i = rows; i >= 1; i--)
        {
            // Printing leading spaces
            for (int j = 1; j <= rows - i; j++)
            {
                System.out.print(" ");
            }

            // Printing pattern
            for (int j = 1; j <= i; j++)
            {
                // Print '*' at the borders and for the first and last row
                if (j == 1 || j == i || i == rows)
                {
                    System.out.print("* ");
                }
                else
                {
                    System.out.print("  ");
                }
            }

            System.out.println();
        }

        // Printing lower half of the pattern
        for (int i = 2; i <= rows; i++)
        {
            // Printing leading spaces
            for (int j = 1; j <= rows - i; j++)
            {
                System.out.print(" ");
            }

            // Printing pattern
            for (int j = 1; j <= i; j++)
            {
                // Print '*' at the borders and for the first and last row
                if (j == 1 || j == i || i == rows)
                {
                    System.out.print("* ");
                }
                else
                {
                    System.out.print("  ");
                }
            }

            System.out.println();
        }

        // Closing the scanner object
        sc.close();
    }
}

Output:

How many rows you want in this pattern?
7
Here is your pattern....!!!
* * * * * * *
 *         *
  *       *
   *     *
    *   *
     * *
      *
     * *
    *   *
   *     *
  *       *
 *         *
* * * * * * *

Explanation:

1. The program starts by asking the user for the number of rows for the pattern using a Scanner object.

2. The for loop for the upper half starts with the full number of stars and decreases by one in each subsequent row, and an inner loop prints the leading spaces.

3. Within the pattern printing loop, conditional checks are performed to print an asterisk only if it's the first or last position in the row, or if it's the first or last row itself.

4. The lower half is printed in a similar manner, but the number of stars increases from one to the full number, with the same conditions for printing asterisks.

5. After printing the pattern, the Scanner object is closed to prevent resource leaks.


Comments