Post thumbnail
JAVA

Master These 10 Star Patterns in Java for Your Next Interview

By Lavish Jain

Table of contents


  1. A Deep Dive Into Star Patterns
    • Right Triangle Star Pattern
    • Inverted Right Triangle Star Pattern
    • Thinking Beyond Patterns
    • Pyramid Star Pattern
    • Hollow Pyramid Star Pattern
    • Full Pyramid Star Pattern
    • Diamond Star Pattern
    • Right Pascal's Triangle
    • Hollow Diamond Star Pattern
    • Left Triangle Star Pattern
    • Sandglass Star Pattern
  2. Wrapping It Up

A Deep Dive Into Star Patterns 

Are you ready to take your Java skills to the next level? Understanding how to print star patterns can be a surprisingly effective way to boost your programming proficiency. These simple yet powerful patterns not only help sharpen your logic but also give you an edge in coding interviews. Star patterns provide an engaging challenge, forcing you to break down problems into smaller, manageable steps. Whether you’re preparing for an interview or just honing your coding expertise, mastering these patterns will give you the confidence to solve more complex problems. 

Let’s dive into 10 essential star patterns in Java that are not only easy to implement but also crucial to your growth as a developer. These patterns are more than just code; they’re a way to think critically and creatively, and we’ll break each one down step-by-step to help you ace your next coding challenge.

1. Right Triangle Star Pattern

Code:

import java.util.Scanner;

public class RightTriangleStarPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print(“Enter number of rows: “);
        int rows = scanner.nextInt();

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }
    }
}

Test Case Input:

Enter number of rows: 5

Output:

*

**

***

****

*****

Logic Explanation:

  • Outer loop runs from 1 to rows for each row.
  • Inner loop runs from 1 to i to print *.
  • After each inner loop iteration, System.out.println() creates a new line.

2. Inverted Right Triangle Star Pattern

Code:

import java.util.Scanner;

public class InvertedRightTriangleStarPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print(“Enter number of rows: “);
        int rows = scanner.nextInt();

        for (int i = rows; i >= 1; i–) {
            for (int j = 1; j <= i; j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }
    }
}

Test Case Input:

Enter number of rows: 5

Output:

*****

****

***

**

*

Logic Explanation:

  • Outer loop runs from rows down to 1.
  • Inner loop runs from 1 to i to print * for each row.
  • Decreasing i reduces the number of * printed in each row.

Thinking Beyond Patterns

Mastering star patterns is a great way to build logic, but what’s next? If you’re aiming to become a well-rounded Java developer, it’s time to go beyond just patterns and tackle real-world applications. Understanding full-stack development can open doors to high-paying opportunities, and Java plays a crucial role in it.

The demand for Java Full-Stack Developers is soaring, and if you’re serious about your coding career, now’s the perfect time to level up. Check out this Java Full-Stack Development Program designed to take you from beginner to job-ready with hands-on projects, expert mentorship, and placement assistance.

Now, let’s move on to the next exciting pattern!

3. Pyramid Star Pattern

Code:

import java.util.Scanner;

public class PyramidStarPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print(“Enter number of rows: “);
        int rows = scanner.nextInt();

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows – i; j++) {
                System.out.print(” “);
            }
            for (int j = 1; j <= 2 * i – 1; j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }
    }
}

Test Case Input:

Enter number of rows: 4

Output:

   *

  ***

 *****

*******

Logic Explanation:

  • Outer loop handles rows, inner loops manage spaces and stars.
  • First inner loop prints spaces for alignment, and second prints stars for the pyramid shape.
MDN

4. Hollow Pyramid Star Pattern

Code:

import java.util.Scanner;
public class HollowPyramidStarPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print(“Enter number of rows: “);
        int rows = scanner.nextInt();

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows – i; 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();
        }
    }
}

Test Case Input:

Enter number of rows: 4

Output:

   *

  * *

 *   *

*******

Logic Explanation:

  • Adds a condition to print * only for the edges and base of the pyramid.

5. Full Pyramid Star Pattern

Code:

import java.util.Scanner;

public class FullPyramidStarPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print(“Enter number of rows: “);
        int rows = scanner.nextInt();

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows – i; j++) {
                System.out.print(” “);
            }
            for (int j = 1; j <= 2 * i – 1; j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }
        for (int i = rows – 1; i >= 1; i–) {
            for (int j = 1; j <= rows – i; j++) {
                System.out.print(” “);
            }
            for (int j = 1; j <= 2 * i – 1; j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }
    }
}

Test Case Input:

Enter number of rows: 4

Output:

   *

  ***

 *****

*******

 *****

  ***

   *

Logic Explanation:

  • Combines an upward pyramid and a downward pyramid.

6. Diamond Star Pattern

Code:

import java.util.Scanner;

public class DiamondStarPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print(“Enter number of rows: “);
        int rows = scanner.nextInt();

        // Upper pyramid
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows – i; j++) {
                System.out.print(” “);
            }
            for (int j = 1; j <= 2 * i – 1; j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }

        // Lower inverted pyramid
        for (int i = rows – 1; i >= 1; i–) {
            for (int j = 1; j <= rows – i; j++) {
                System.out.print(” “);
            }
            for (int j = 1; j <= 2 * i – 1; j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }
    }
}

Test Case Input:

Enter number of rows: 4

Output:

   *

  ***

 *****

*******

 *****

  ***

   *

Logic Explanation:

  • Combines two patterns: an upward pyramid and an inverted pyramid, creating a diamond shape.

7. Right Pascal’s Triangle

Code:

import java.util.Scanner;

public class RightPascalsTriangle {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print(“Enter number of rows: “);
        int rows = scanner.nextInt();

        // Upper part
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }

        // Lower inverted part
        for (int i = rows – 1; i >= 1; i–) {
            for (int j = 1; j <= i; j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }
    }
}

Test Case Input:

Enter number of rows: 4

Output:

*

**

***

****

***

**

*

Logic Explanation:

  • Upper part is a right triangle, and the lower part is an inverted right triangle.

8. Hollow Diamond Star Pattern

Code:

import java.util.Scanner;

public class HollowDiamondStarPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print(“Enter number of rows: “);
        int rows = scanner.nextInt();

        // Upper hollow pyramid
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows – 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();
        }

        // Lower hollow inverted pyramid
        for (int i = rows – 1; i >= 1; i–) {
            for (int j = 1; j <= rows – 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();
        }
    }
}

Test Case Input:

Enter number of rows: 4

Output:

   *

  * *

 *   *

*     *

 *   *

  * *

   *

Logic Explanation:

  • Adds spaces inside the diamond except for the edges to create a hollow effect.

9. Left Triangle Star Pattern

Code:

import java.util.Scanner;

public class LeftTriangleStarPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print(“Enter number of rows: “);
        int rows = scanner.nextInt();

        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows – i; j++) {
                System.out.print(” “);
            }
            for (int j = 1; j <= i; j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }
    }
}

Test Case Input:

Enter number of rows: 4

Output:

   *

  **

 ***

****

Logic Explanation:

  • Adjusts spaces for alignment to create a left-oriented triangle pattern.

10. Sandglass Star Pattern

Code:

import java.util.Scanner;

public class SandglassStarPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print(“Enter number of rows: “);
        int rows = scanner.nextInt();

        // Upper inverted pyramid
        for (int i = rows; i >= 1; i–) {
            for (int j = 1; j <= rows – i; j++) {
                System.out.print(” “);
            }
            for (int j = 1; j <= 2 * i – 1; j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }

        // Lower pyramid
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= rows – i; j++) {
                System.out.print(” “);
            }
            for (int j = 1; j <= 2 * i – 1; j++) {
                System.out.print(“*”);
            }
            System.out.println();
        }
    }
}

Test Case Input:

Enter number of rows: 4

Output:

*******

 *****

  ***

   *

   *

  ***

 *****

*******

Logic Explanation:

  • Combines an inverted pyramid on top and a regular pyramid below, forming a sandglass pattern.

Also Read: 40 Java Interview Questions for Freshers with Clear & Concise Answers

MDN

Wrapping It Up

Mastering these 10 star patterns in Java doesn’t just prepare you for your next interview—it sharpens your programming fundamentals, enhances problem-solving abilities, and teaches you the importance of structure in coding. Each pattern demonstrates key concepts like loops, conditional statements, and space management, which are pivotal for any programmer. 

As you practice, you’ll start noticing how these concepts apply to more complex algorithms and real-world applications. Remember, coding is all about breaking down problems and finding efficient solutions. By consistently tackling patterns like these, you’ll be well on your way to becoming a more proficient coder. Keep experimenting, learning, and coding!

Here’s your bonus – If you’re preparing for a Java developer interview, it’s crucial to know what kind of questions you might face. Here’s a detailed list of common Java interview questions that can help you refine your approach: Java Developer Interview Questions.

Career transition

Did you enjoy this article?

Schedule 1:1 free counselling

Similar Articles

Loading...
Share logo Copy link
Power Packed Webinars
Free Webinar Icon
Power Packed Webinars
Subscribe now for FREE! 🔔
close
Webinar ad
Table of contents Table of contents
Table of contents Articles
Close button

  1. A Deep Dive Into Star Patterns
    • Right Triangle Star Pattern
    • Inverted Right Triangle Star Pattern
    • Thinking Beyond Patterns
    • Pyramid Star Pattern
    • Hollow Pyramid Star Pattern
    • Full Pyramid Star Pattern
    • Diamond Star Pattern
    • Right Pascal's Triangle
    • Hollow Diamond Star Pattern
    • Left Triangle Star Pattern
    • Sandglass Star Pattern
  2. Wrapping It Up