SF Learners Hub · Blog · About · Contact

Star Pattern Programs in Java: 25+ Most Asked Star Pattern Interview Questions for Freshers

By Aman Garg · Published · Interview Questions

Star Pattern Programs in Java: 25+ Most Asked Star Pattern Interview Questions for Freshers

Star pattern programs are among the most popular beginner-level coding questions asked in fresher interviews. Whether you are preparing for a Java Developer, Software Developer, Salesforce Developer, or other programming-related role, understanding star patterns will help you strengthen your programming fundamentals.

These programs may look simple, but they test several important concepts:

  • Nested loops

  • For loops and while loops

  • Conditional statements

  • Row and column relationships

  • Space and star calculations

  • Logical and analytical thinking

In this blog, we will explore 25+ commonly asked star pattern programs in Java, starting from basic patterns and progressing towards advanced patterns.

1. Understand the Basic Logic Behind Star Patterns

Before solving star pattern problems, understand three important concepts.

1. Outer Loop – Rows

The outer loop controls how many rows will be printed.

2. Inner Loop – Columns

The inner loop controls how many stars or spaces will be printed in each row.

3. Print Statement

  • System.out.print("*") prints stars on the same line.

  • System.out.println() moves the cursor to the next line.

Basic Java Structure

public class StarPattern {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

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

            System.out.println();
        }
    }
}

Output:

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

2. Square Star Pattern

Interview Question: Write a Java program to print a square star pattern.

Output:

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

Logic:

  • Run the outer loop five times.

  • Run the inner loop five times for every row.

  • Print stars in every column.

Java Code:

public class SquarePattern {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

3. Right-Angled Triangle Pattern

Output:

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

Logic:

  • Outer loop represents rows.

  • Inner loop runs from 1 to the current row number.

  • Every new row contains one additional star.

Java Code:

public class RightTriangle {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

4. Inverted Right-Angled Triangle

Output:

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

Logic:

  • Start with five stars.

  • Decrease the number of stars by one in every row.

  • The inner loop runs from the current row to n.

Java Code:

public class InvertedTriangle {
    public static void main(String[] args) {

        int n = 5;

        for (int i = n; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

5. Left-Aligned Inverted Triangle

Output:

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

Logic:

  • Print spaces before stars.

  • Increase spaces with every row.

  • Decrease stars with every row.

Java Code:

public class LeftInvertedTriangle {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

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

            for (int j = i; j <= n; j++) {
                System.out.print("* ");
            }

            System.out.println();
        }
    }
}

6. Right-Aligned Triangle

Output:

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

Logic:

  • Print spaces before stars.

  • Spaces decrease as rows increase.

  • Stars increase as rows increase.

Java Code:

public class RightAlignedTriangle {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

            for (int j = i; j < n; j++) {
                System.out.print("  ");
            }

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

            System.out.println();
        }
    }
}

Pyramid Star Patterns

7. Full Pyramid Pattern

Output:

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

Logic:

  • First print spaces.

  • Then print odd numbers of stars: 1, 3, 5, 7, 9.

  • The number of stars in each row follows 2 * i - 1.

Java Code:

public class PyramidPattern {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

            for (int j = i; j < n; j++) {
                System.out.print("  ");
            }

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

            System.out.println();
        }
    }
}

8. Inverted Pyramid Pattern

Output:

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

Logic:

  • Start with the maximum number of stars.

  • Reduce stars by two in each row.

  • Increase spaces in every row.

Java Code:

public class InvertedPyramid {
    public static void main(String[] args) {

        int n = 5;

        for (int i = n; i >= 1; i--) {

            for (int j = n; j > i; j--) {
                System.out.print("  ");
            }

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

            System.out.println();
        }
    }
}

9. Diamond Star Pattern

Output:

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

Logic:
A diamond combines two patterns:

  1. Pyramid pattern.

  2. Inverted pyramid pattern.

Java Code:

public class DiamondPattern {
    public static void main(String[] args) {

        int n = 5;

        // Upper half
        for (int i = 1; i <= n; i++) {

            for (int j = i; j < n; j++) {
                System.out.print("  ");
            }

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

            System.out.println();
        }

        // Lower half
        for (int i = n - 1; i >= 1; i--) {

            for (int j = n; j > i; j--) {
                System.out.print("  ");
            }

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

            System.out.println();
        }
    }
}

10. Hollow Pyramid Pattern

Output:

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

Logic:

  • Print spaces before the pyramid.

  • Print stars at the first and last positions.

  • Print spaces between the boundary stars.

  • Print the complete bottom row.

Java Code:

public class HollowPyramid {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

            for (int j = i; j < n; j++) {
                System.out.print("  ");
            }

            for (int j = 1; j <= 2 * i - 1; j++) {

                if (j == 1 || j == 2 * i - 1 || i == n) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }

            System.out.println();
        }
    }
}

Hollow Star Patterns

11. Hollow Square Pattern

Output:

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

Logic:
Print stars when:

  • It is the first row.

  • It is the last row.

  • It is the first column.

  • It is the last column.

Otherwise, print spaces.

Java Code:

public class HollowSquare {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= n; j++) {

                if (i == 1 || i == n || j == 1 || j == n) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }

            System.out.println();
        }
    }
}

12. Hollow Right-Angled Triangle

Output:

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

Logic:
Print stars at the first column, diagonal boundary, and last row.

Java Code:

public class HollowRightTriangle {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= i; j++) {

                if (j == 1 || j == i || i == n) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }

            System.out.println();
        }
    }
}

13. Hollow Diamond Pattern

Output:

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

Logic:

  • Combine upper and lower diamond halves.

  • Print stars at the left and right boundaries.

  • Print spaces inside.

  • Manage the number of leading spaces in each half.

Java Code:

public class HollowDiamond {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

            for (int j = i; j < n; 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();
        }

        for (int i = n - 1; i >= 1; i--) {

            for (int j = n; 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();
        }
    }
}

Advanced Star Patterns

14. Butterfly Star Pattern

Output:

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

Logic:
A butterfly pattern consists of two wings.

  • Left wing stars increase in the upper half.

  • Right wing stars increase in the upper half.

  • Middle spaces decrease.

  • Reverse the same logic for the lower half.

Java Code:

public class ButterflyPattern {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= i; j++)
                System.out.print("* ");

            for (int j = 1; j <= 2 * (n - i); j++)
                System.out.print("  ");

            for (int j = 1; j <= i; j++)
                System.out.print("* ");

            System.out.println();
        }

        for (int i = n - 1; i >= 1; i--) {

            for (int j = 1; j <= i; j++)
                System.out.print("* ");

            for (int j = 1; j <= 2 * (n - i); j++)
                System.out.print("  ");

            for (int j = 1; j <= i; j++)
                System.out.print("* ");

            System.out.println();
        }
    }
}

15. Hourglass Star Pattern

Output:

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

Logic:

  • Start with the maximum number of stars.

  • Decrease stars in the upper half.

  • Increase stars in the lower half.

  • Adjust spaces to maintain alignment.

Java Code:

public class HourglassPattern {
    public static void main(String[] args) {

        int n = 5;

        for (int i = n; i >= 1; i--) {

            for (int j = n; j > i; j--)
                System.out.print("  ");

            for (int j = 1; j <= 2 * i - 1; j++)
                System.out.print("* ");

            System.out.println();
        }

        for (int i = 2; i <= n; i++) {

            for (int j = n; j > i; j--)
                System.out.print("  ");

            for (int j = 1; j <= 2 * i - 1; j++)
                System.out.print("* ");

            System.out.println();
        }
    }
}

16. X Star Pattern

Output:

*       *
  *   *
    *
  *   *
*       *

Logic:
Print stars when the row and column positions match either diagonal:

  • i == j

  • i + j == n + 1

Java Code:

public class XPattern {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= n; j++) {

                if (i == j || i + j == n + 1) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }

            System.out.println();
        }
    }
}

17. Plus (+) Star Pattern

Output:

    *
    *
* * * * *
    *
    *

Logic:
Print a star when:

  • The row is the middle row.

  • The column is the middle column.

Java Code:

public class PlusPattern {
    public static void main(String[] args) {

        int n = 5;
        int mid = (n + 1) / 2;

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= n; j++) {

                if (i == mid || j == mid) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }

            System.out.println();
        }
    }
}

18. Right Arrow Star Pattern

Output:

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

Logic:

  • Increase stars in the first half.

  • Decrease stars in the second half.

  • Use two separate loops.

Java Code:

public class ArrowPattern {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }

        for (int i = n - 1; i >= 1; i--) {
            for (int j = 1; j <= i; j++) {
                System.out.print("* ");
            }
            System.out.println();
        }
    }
}

Number and Alphabet Patterns

Interviewers sometimes combine star patterns with numbers and alphabets to test whether candidates understand nested loops and conditions.

19. Number Triangle Pattern

Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Java Code:

public class NumberTriangle {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

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

            System.out.println();
        }
    }
}

20. Repeated Number Triangle

Output:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Java Code:

public class RepeatedNumber {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

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

            System.out.println();
        }
    }
}

21. Alphabet Triangle Pattern

Output:

A
A B
A B C
A B C D
A B C D E

Java Code:

public class AlphabetPattern {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= i; j++) {
                System.out.print((char)('A' + j - 1) + " ");
            }

            System.out.println();
        }
    }
}

22. Continuous Number Triangle

Output:

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Java Code:

public class ContinuousNumber {
    public static void main(String[] args) {

        int n = 5;
        int count = 1;

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j <= i; j++) {
                System.out.print(count + " ");
                count++;
            }

            System.out.println();
        }
    }
}

23. Pyramid Number Pattern

Output:

        1
      1 2 1
    1 2 3 2 1
  1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

Java Code:

public class NumberPyramid {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

            for (int j = i; j < n; j++) {
                System.out.print("  ");
            }

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

            for (int j = i - 1; j >= 1; j--) {
                System.out.print(j + " ");
            }

            System.out.println();
        }
    }
}

24. Sandglass Pattern

Output:

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

Java Code:

public class SandglassPattern {
    public static void main(String[] args) {

        int n = 5;

        for (int i = 1; i <= n; i++) {

            for (int j = 1; j < i; j++)
                System.out.print("  ");

            for (int j = i; j <= n; j++)
                System.out.print("* ");

            System.out.println();
        }

        for (int i = n - 1; i >= 1; i--) {

            for (int j = 1; j < i; j++)
                System.out.print("  ");

            for (int j = i; j <= n; j++)
                System.out.print("* ");

            System.out.println();
        }
    }
}

25. Hollow Butterfly Pattern

Output:

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

Logic:
Print stars at the boundaries of both wings and leave spaces between the inner positions. Combine the upper and lower halves.

26. Hollow Hourglass Pattern

Output:

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

Logic:

  • Print the full first and last rows.

  • Print boundary stars in the remaining rows.

  • Adjust leading and internal spaces.

Common Interview Questions About Star Patterns

Apart from writing programs, interviewers may ask questions to understand your logic.

Q1. What is a nested loop?

A nested loop is a loop placed inside another loop. In star patterns, the outer loop generally controls rows and the inner loop controls columns.

Q2. Why do we use println() after the inner loop?

It moves the cursor to the next line after completing the current row.

Q3. What is the difference between print() and println()?

  • print() prints output on the same line.

  • println() Prints output and moves to the next line.

Q4. How do you print a right-aligned triangle?

Use one loop to print leading spaces and another loop to print stars.

Q5. How do you create a hollow pattern?

Use conditional statements to identify boundary positions and print stars only at those positions.

Q6. How do you calculate the number of stars in a pyramid?

For rowi, the number of stars is:

2 * i - 1

Q7. What is the time complexity of a basic square pattern?

For an n × n square pattern, the time complexity is O(n²), because the nested loops execute n × n times.

Q8. How can you approach an unknown star pattern in an interview?

Break the pattern into rows, identify spaces and stars in each row, find the relationship between row and column numbers, and then implement the nested loops.

Tips to Solve Star Pattern Questions in Interviews

Follow these steps whenever an interviewer asks you to create a new pattern.

  1. Observe the output: Identify the number of rows and columns.

  2. Identify the spaces: Determine whether leading spaces or internal spaces are required.

  3. Count the stars: Calculate how many stars are printed in each row.

  4. Find the relationship: Identify whether stars increase, decrease, or remain constant.

  5. Write the outer loop: Decide how many rows are required.

  6. Write the inner loops: Use separate loops for spaces and stars when required.

  7. Use conditions: Apply if-else conditions for hollow and special patterns.

  8. Dry run your program: Execute it manually for the first few rows.

Practice Questions for Freshers

Try solving these questions without looking at the solutions.

Beginner Level

  • Print a square star pattern.

  • Print a right-angled triangle.

  • Print an inverted triangle.

  • Print a left-aligned triangle.

  • Print a right-aligned triangle.

Intermediate Level

  • Print a full pyramid.

  • Print an inverted pyramid.

  • Print a diamond pattern.

  • Print a hollow square.

  • Print a hollow triangle.

  • Print a hollow pyramid.

  • Print a number pyramid.

Advanced Level

  • Print a butterfly pattern.

  • Print an hourglass pattern.

  • Print an X pattern.

  • Print a plus pattern.

  • Print a hollow diamond.

  • Print a hollow butterfly.

  • Print a hollow hourglass.

Conclusion

Star pattern programming is one of the best ways to improve your logical thinking and strengthen your understanding of loops in Java.

For freshers, the goal should not be to memorize every pattern. Instead, understand how rows, columns, spaces, and conditions work together.

Once you understand the basic logic, you can solve even complex patterns by breaking them into smaller.

Interview Preparation Tip: Practice at least 5 patterns daily, dry-run your code, and try to explain the logic aloud as if you are answering an interviewer.

Keep practicing, keep coding, and build strong programming fundamentals!

Happy Coding! 🚀

Related articles