Star Pattern programs in Java

Star pattern in java

In this article, we will see how to print star pattern in java. Pattern printing programs are good for logic building in programming. We will discuss different types of Star pattern and see the logic behind printing it.

Square Star Pattern in java

Given a number n, we need to print a Square Star Pattern with n number of rows and columns, as in a Square the length of each side is same. Let us look at the program.

Output:

Hollow Square Star Pattern in java

Now, this pattern is more or less same as above, the condition is we print ‘*’ only at the boundaries of the Square. Otherwise we print space. Let us look at the code.

Output:

Hollow Square Pattern with Diagonal in java

Now, this pattern is same as above, along with the hollow square we need to print ‘*’ along each diagonal. When the value of row and column index are equal we print ‘*’otherwise we print blank space. Let us look at the code:

Output:

Rhombus Star Pattern in java

Given n number of rows, we need to print a Rhombus Star Pattern. Let us look at the program.

Output:

Note: If we want to print its Mirror, then in the loop which prints Spaces we change the Loop Condition from k < n – i to k < i.

Right Angled Triangle Star Pattern in java

We need to print a Right Angled Triangle for a given number of rows.

Output:

Hollow Mirrored Right Angled Triangle Star Pattern in java

We will print the Right Angled Triangle like above but we will print the Mirror of it with ‘*’ pattern along the boundary and the inside will be hollow.

Output:

Hollow Inverted Right Angled Triangle star Pattern in java

We will print the Inverted Right Angle Triangle which will be hollow from inside for a given number of rows. So, we will again print only the boundary of the inverted triangle. Let us look at the code.

Output:

Equilateral Triangle Star Pattern in java

For a given number of rows, we print an Equilateral triangle where each row will have same number of stars as the row number. E.g. Row 1 will have 1 Star, Row 2 will have 2 Stars and so on. Let us look at the code for this:

Output:

Pyramid Star Pattern in java

For a given number of rows, we have to print the Pyramid Star Pattern. The first Row will have only 1 star, every other row will have No. of stars = Stars in Previous Row + 2. Example: Row 2 will have 3 Stars. Row 3 will have 5 stars and so on. The code is shown below:

Output:

Inverted Pyramid Star Pattern in java

We will print the Inverted version of the above pattern, for n number of rows. The catch is we need a decremental loop here while printing ‘*’ for each row. Let us now look at the program for it.

Output:

Hollow Pyramid Star Pattern in java

For n number of rows, we print the Pyramid Pattern. Now, the pattern will be hollow so it will cover only the boundary regions of the Pyramid. Let us look at the code for this.

Output:

Half-Diamond Star Pattern in java

We need to print a Half Diamond pattern for a given number of rows. If we breakdown this problem we need to print a Right Angled Triangle for n rows and then print the Inverted Right Angled triangle for n-1 rows. Let us look at the program to print such a pattern.

Output:

Diamond Star Pattern in java

We have to print a Diamond Star Pattern for n (n is Odd) number of rows. In simpler terms, we need to print a Pyramid Pattern for n/2 +1 rows (as n is odd), then we print Inverted Pyramid Pattern for the remaining half of the rows. Let us look at the code.

Output:

Left Arrow Star Pattern in java

For a given n number of rows (n is strictly Odd) we have to print a Left Arrow Star Pattern. The logic we use will be same as used in printing Right angled triangle Pattern and we maintain indentation while printing each row. Let us look at the code.

Output:

Right Arrow Star Pattern in java

Like the above pattern, given n number of rows (n is strictly Odd) we have to print a Right Arrow Star Pattern. The logic we use will be same as used in printing Inverted Right angled triangle Pattern and we maintain indentation while printing each row. Let us look at the program to print such pattern.

Output:

Cross or X Star Pattern in java

Given n number of rows we need to print a Cross or X Star Pattern of 2*n -1 rows. For Ex: If the Number of Rows given in Input = 9, then we need to print a X Star Pattern with 17 (2*n-1) rows . The program for it is shown below:

Output:

Hollow Diamond Star Pattern in java

Given n number of rows, we need to print a Hollow Diamond Star pattern enclosed within a block of 2*n number of rows. So, for 5 rows the Program should print 10 rows of Star pattern enclosing a Hollow Diamond. Let us look at the code for it.

Output:

Christmas Tree Star Pattern in java

For a given height h, we need to print a Christmas Tree Pattern. The pattern consists of h Pyramids which will shape the upper tree and at the end we print the trunk or branch of the tree of h-1 height. Let us look at the code to print this pattern.

Output:

So that’s it for the article you can try out the above examples with different inputs to test your understanding. You can leave your suggestions or doubts in comment section below.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *