10 Star Patterns in Java: A Must-Know Interview Question

Harish
5 min readApr 2, 2024

--

Image Credits to https://learn2code.tech/

Star pattern programs are classic interview questions that test a candidate’s understanding of loops and basic programming logic. In this blog post, we’ll dive into some common star pattern programs in Java, explaining the logic behind each pattern and providing code examples.

1.Printing a Right Triangle Star Pattern:

*
**
***
****
*****
public class RightTriangleStarPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

Inverted Right Triangle Star Pattern:

*****
****
***
**
*
public class InvertedRightTriangleStarPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; --i) {
for (int j = 1; j <= i; ++j) {
System.out.print("*");
}
System.out.println();
}
}
}

3.Pyramid Star Pattern:

    *
***
*****
*******
*********
public class PyramidStarPattern {
public static void main(String[] args) {
int rows = 5, k = 0;
for (int i = 1; i <= rows; ++i, k = 0) {
for (int space = 1; space <= rows - i; ++space) {
System.out.print(" ");
}
while (k != 2 * i - 1) {
System.out.print("*");
++k;
}
System.out.println();
}
}
}

4.Hollow Pyramid Star Pattern:

    *
* *
* *
* *
* * * * *
public class HollowPyramidStarPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
for (int space = 1; space <= rows - i; ++space) {
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();
}
}
}

For more star pattern interview questions and programs, click here.

5.Full Pyramid Star Pattern:

    *
***
*****
*******
*********
*******
*****
***
*
public class FullPyramidStarPattern {
public static void main(String[] args) {
int rows = 5, spaces = 0;
for (int i = 1; i <= rows; ++i, spaces = 0) {
for (int space = 1; space <= rows - i; ++space) {
System.out.print(" ");
++spaces;
}
while (spaces != 0) {
System.out.print(" ");
--spaces;
}
for (int j = 1; j <= 2 * i - 1; ++j) {
System.out.print("*");
}
System.out.println();
}
spaces = 1;
for (int i = 1; i <= rows - 1; ++i, ++spaces) {
for (int space = 1; space <= spaces; ++space) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * (rows - i) - 1; ++j) {
System.out.print("*");
}
System.out.println();
}
}
}

6.Diamond Star Pattern:

    *
***
*****
*******
*********
*******
*****
***
*
public class DiamondStarPattern {
public static void main(String[] args) {
int rows = 5, spaces = 0;
for (int i = 1; i <= rows; ++i, spaces = 0) {
for (int space = 1; space <= rows - i; ++space) {
System.out.print(" ");
++spaces;
}
while (spaces != 0) {
System.out.print(" ");
--spaces;
}
for (int j = 1; j <= 2 * i - 1; ++j) {
System.out.print("*");
}
System.out.println();
}
spaces = 1;
for (int i = 1; i <= rows - 1; ++i, ++spaces) {
for (int space = 1; space <= spaces; ++space) {
System.out.print(" ");
}
for (int j = 1; j <= 2 * (rows - i) - 1; ++j) {
System.out.print("*");
}
System.out.println();
}
}
}

7.Hollow Diamond Star Pattern:

    *
* *
* *
* *
* *
* *
* *
* *
*
public class HollowDiamondStarPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
for (int space = 1; space <= rows - i; ++space) {
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();
}
for (int i = rows - 1; i >= 1; --i) {
for (int space = 1; space <= rows - i; ++space) {
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();
}
}
}

8.Rhombus Star Pattern:

    *****
*****
*****
*****
*****
public class RhombusStarPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
for (int space = 1; space <= rows - i; ++space) {
System.out.print(" ");
}
for (int j = 1; j <= rows; ++j) {
System.out.print("*");
}
System.out.println();
}
}
}

8.Hollow Rhombus Star Pattern:

    *****
* *
* *
* *
*****
public class HollowRhombusStarPattern {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; ++i) {
for (int space = 1; space <= rows - i; ++space) {
System.out.print(" ");
}
for (int j = 1; j <= rows; ++j) {
if (i == 1 || i == rows || j == 1 || j == rows)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}

9.Cross Star Pattern:

*   *
* *
*
* *
* *
public class CrossStarPattern {
public static void main(String[] args) {
int size = 5;
for (int i = 1; i <= size; ++i) {
for (int j = 1; j <= size; ++j) {
if (j == i || j == size - i + 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}

10.Butterfly Star Pattern:

*        *
** **
*** ***
**** ****
**********
**********
**** ****
*** ***
** **
* *
public class ButterflyStarPattern {
public static void main(String[] args) {
int size = 5;
for (int i = 1; i <= size; ++i) {
for (int j = 1; j <= i; ++j) {
System.out.print("*");
}
int spaces = 2 * size - 2 * i;
for (int j = 1; j <= spaces; ++j) {
System.out.print(" ");
}
for (int j = 1; j <= i; ++j) {
System.out.print("*");
}
System.out.println();
}
for (int i = size; i >= 1; --i) {
for (int j = 1; j <= i; ++j) {
System.out.print("*");
}
int spaces = 2 * size - 2 * i;
for (int j = 1; j <= spaces; ++j) {
System.out.print(" ");
}
for (int j = 1; j <= i; ++j) {
System.out.print("*");
}
System.out.println();
}
}
}

For more star pattern interview questions and programs, click here.

--

--

No responses yet