15 Java Star Pattern Programs

Introduction

Star pattern programs are one of the most popular topics in Java programming. These patterns are often asked in coding interviews and help in understanding the basics of loops and nested loops. Star patterns can take various shapes, such as triangles, squares, pyramids, etc. In this post, we'll cover 15 important star pattern programs in Java and a detailed explanation of each program.


1. Right-Angled Triangle Star Pattern

A simple triangle pattern where stars form a right-angled triangle with the right angle on the bottom-left. This is one of the most basic star patterns and is a good starting point for beginners.

Example:

*
**
***
****
*****

Java Program:

public class RightAngledTriangle {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Outer loop for rows
        for (int i = 1; i <= rows; i++) {
            // Inner loop to print '*' i times
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • The outer loop (i) controls the number of rows.
  • The inner loop (j) prints i stars for each row.
  • After printing stars, System.out.println() is used to move to the next line.

2. Inverted Right-Angled Triangle Star Pattern

This is the reverse of the right-angled triangle pattern. The stars form a right-angled triangle but with the right angle at the top-left corner.

Example:

*****
****
***
**
*

Java Program:

public class InvertedRightAngledTriangle {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Outer loop for rows
        for (int i = rows; i >= 1; i--) {
            // Inner loop to print '*' i times
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • The outer loop (i) starts from rows and decreases until it reaches 1.
  • The inner loop prints stars for each row, decreasing by one in each iteration.

3. Pyramid Star Pattern

This pattern prints a pyramid of stars that is centered and symmetrical. The number of stars increases as we move down the rows.

Example:

    *
   ***
  *****
 *******
*********

Java Program:

public class Pyramid {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Outer loop for rows
        for (int i = 1; i <= rows; i++) {
            // Print leading spaces
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }

            // Print stars for each row
            for (int j = 1; j <= (2 * i - 1); j++) {
                System.out.print("*");
            }

            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • The first inner loop prints spaces for centering the stars.
  • The second inner loop prints stars, increasing the number of stars in each row.

4. Inverted Pyramid Star Pattern

An inverted version of the pyramid star pattern where the stars start wide at the top and gradually reduce as we move down the rows.

Example:

*********
 *******
  *****
   ***
    *

Java Program:

public class InvertedPyramid {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Outer loop for rows
        for (int i = rows; i >= 1; i--) {
            // Print leading spaces
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }

            // Print stars for each row
            for (int j = 1; j <= (2 * i - 1); j++) {
                System.out.print("*");
            }

            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • The first inner loop prints spaces for alignment.
  • The second inner loop prints stars, decreasing the number of stars in each row.

5. Hollow Square Star Pattern

A hollow square pattern consists of stars on the boundary of the square while the inside is filled with spaces.

Example:

*****
*   *
*   *
*   *
*****

Java Program:

public class HollowSquare {
    public static void main(String[] args) {
        int size = 5; // Size of the square

        // Outer loop for rows
        for (int i = 1; i <= size; i++) {
            // Inner loop for columns
            for (int j = 1; j <= size; j++) {
                // Print '*' for the boundary
                if (i == 1 || i == size || j == 1 || j == size) {
                    System.out.print("*");
                } else {
                    // Print space for hollow part
                    System.out.print(" ");
                }
            }
            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • Stars are printed on the first and last rows, and the first and last columns.
  • For all other positions, spaces are printed to create the hollow effect.

6. Hollow Pyramid Star Pattern

This pattern prints a hollow pyramid where only the boundary of the pyramid is filled with stars, and the inside is hollow.

Example:

    *
   * *
  *   *
 *     *
*********

Java Program:

public class HollowPyramid {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Outer loop for rows
        for (int i = 1; i <= rows; i++) {
            // Print leading spaces
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }

            // Print stars for boundary and spaces for hollow part
            for (int j = 1; j <= (2 * i - 1); j++) {
                if (j == 1 || j == (2 * i - 1) || i == rows) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }

            System.out.println(); // Move to the next line
        }
    }
}

Explanation:

  • The first inner loop prints spaces for centering the pyramid.
  • The second inner loop prints stars for the boundary and spaces for the hollow area.

7. Diamond Star Pattern

A diamond pattern is a combination of two pyramids, one inverted, placed together. It is symmetrical and forms a diamond shape.

Example:

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

Java Program:

public class Diamond {
    public static void main(String[] args) {
        int rows = 5; // Number of rows

        // Print the top pyramid
        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        // Print the inverted pyramid
        for (int i = rows - 1; i >= 1; i--) {
            for (int j = rows; j > i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Explanation:

  • The first loop prints a normal pyramid.
  • The second loop prints an inverted pyramid, starting from one row less than the top pyramid.

8. Right-Left Triangle Star Pattern

This pattern is a combination of a right-angled triangle and its mirror image, forming a "right-left" pattern.

Example:

*        *
**      **
***    ***
****  ****
**********

Java Program:

public class RightLeftTriangle {
    public static void main(String[] args) {
        int rows = 5;

        // Outer loop for rows
        for (int i = 1; i <= rows; i++) {
            // Left triangle
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }

            // Spaces between two triangles
            for (int j = i; j < rows; j++) {
                System.out.print("  ");
            }

            // Right triangle
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }

            System.out.println(); // Move to next line
        }
    }
}

Explanation:

  • The

left triangle is printed using the first inner loop.

  • The second loop prints spaces to separate the two triangles.
  • The right triangle is printed using the third inner loop.

9. Sandglass Star Pattern

This pattern resembles a sandglass or hourglass shape, formed by an inverted triangle at the top and a normal triangle at the bottom.

Example:

*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********

Java Program:

public class Sandglass {
    public static void main(String[] args) {
        int rows = 5;

        // Print the upper inverted pyramid
        for (int i = rows; i >= 1; i--) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                System.out.print("*");
            }
            System.out.println();
        }

        // Print the lower pyramid
        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

Explanation:

  • The first loop prints an inverted pyramid.
  • The second loop prints a normal pyramid below it.

10. Left-Aligned Triangle Star Pattern

This pattern prints a left-aligned triangle of stars.

Example:

*
**
***
****
*****

Java Program:

public class LeftAlignedTriangle {
    public static void main(String[] args) {
        int rows = 5;

        // Outer loop for rows
        for (int i = 1; i <= rows; i++) {
            // Inner loop to print '*' i times
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

11. Inverted Left-Aligned Triangle

This pattern prints an inverted left-aligned triangle of stars.

Example:

*****
****
***
**
*

Java Program:

public class InvertedLeftAlignedTriangle {
    public static void main(String[] args) {
        int rows = 5;

        // Outer loop for rows
        for (int i = rows; i >= 1; i--) {
            // Inner loop to print '*' i times
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

12. Right-Aligned Triangle Star Pattern

A triangle that is aligned to the right side.

Example:

    *
   **
  ***
 ****
*****

Java Program:

public class RightAlignedTriangle {
    public static void main(String[] args) {
        int rows = 5;

        // Outer loop for rows
        for (int i = 1; i <= rows; i++) {
            // Print spaces for alignment
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }

            // Print stars
            for (int j = 1; j <= i; j++) {
                System.out.print("*");
            }

            System.out.println();
        }
    }
}

13. Hollow Diamond Star Pattern

A hollow diamond pattern where only the boundary of the diamond is filled with stars, and the inside is hollow.

Example:

    *
   * *
  *   *
 *     *
*       *
 *     *
  *   *
   * *
    *

Java Program:

public class HollowDiamond {
    public static void main(String[] args) {
        int rows = 5;

        // Print the upper half of the diamond
        for (int i = 1; i <= rows; i++) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                if (j == 1 || j == (2 * i - 1)) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }

        // Print the lower half of the diamond
        for (int i = rows - 1; i >= 1; i--) {
            for (int j = rows; j > i; j--) {
                System.out.print(" ");
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                if (j == 1 || j == (2 * i - 1)) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

14. Hollow Inverted Pyramid

An inverted pyramid pattern that is hollow inside.

Example:

*********
 *     *
  *   *
   * *
    *

Java Program:

public class HollowInvertedPyramid {
    public static void main(String[] args) {
        int rows = 5;

        // Outer loop for rows
        for (int i = rows; i >= 1; i--) {
            for (int j = i; j < rows; j++) {
                System.out.print(" ");
            }
            for (int j = 1; j <= (2 * i - 1); j++) {
                if (j == 1 || j == (2 * i - 1) || i == rows) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

15. Cross (X) Star Pattern

This pattern prints a cross (X) shape using stars, where stars appear only on the diagonals of the grid.

Example:

*       *
 *     *
  *   *
   * *
    *
   * *
  *   *
 *     *
*       *

Java Program:

public class CrossPattern {
    public static void main(String[] args) {
        int size = 9;

        // Outer loop for rows
        for (int i = 1; i <= size; i++) {
            for (int j = 1; j <= size; j++) {
                // Print stars on the diagonals
                if (j == i || j == size - i + 1) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
            System.out.println(); // Move to the next line
        }
    }
}

Conclusion

These 15 Java star pattern programs cover a wide variety of important patterns such as triangles, pyramids, hollow shapes, and symmetric designs. Learning to create these patterns helps you to master nested loops and conditional statements and improves problem-solving skills in Java. Experiment with these patterns to become more comfortable with loops and pattern printing!

Comments