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.
Table of Contents
- Square Star Pattern in java
- Hollow Square Star Pattern in java
- Hollow Square Pattern with Diagonal in java
- Rhombus Star Pattern in java
- Right Angled Triangle Star Pattern in java
- Hollow Mirrored Right Angled Triangle Star Pattern in java
- Hollow Inverted Right Angled Triangle star Pattern in java
- Equilateral Triangle Star Pattern in java
- Pyramid Star Pattern in java
- Inverted Pyramid Star Pattern in java
- Hollow Pyramid Star Pattern in java
- Half-Diamond Star Pattern in java
- Diamond Star Pattern in java
- Left Arrow Star Pattern in java
- Right Arrow Star Pattern in java
- Cross or X Star Pattern in java
- Hollow Diamond Star Pattern in java
- Christmas Tree Star Pattern in java
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class PatternExercise { public static void main(String args[]) { int n = 5; //Outer Loop for number of Rows for(int i=0;i<n;i++) { // Inner loop for printing '*' in each column. for(int j=0;j<n;j++) { // We add two spaces to match shape of a square. System.out.print("* "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 |
* * * * * * * * * * * * * * * * * * * * * * * * * |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class PatternExercise { public static void main(String args[]) { int n = 5; //Outer Loop for number of Rows for(int i=0;i<n;i++) { // Inner loop for printing '*' in each column. for(int j=0;j<n;j++) { // For first row and last row we print '*' and for every other row we print the '*' at boundary region. if(i==0 || i==n-1 || j==0 || j==n-1) { System.out.print("* "); } // Otherwise we print blank space. else System.out.print(" "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 |
* * * * * * * * * * * * * * * * |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class PatternExercise { public static void main(String args[]) { int n = 7; //Outer Loop for number of Rows for(int i=0;i<n;i++) { // Inner loop for printing '*' in each column. for(int j=0;j<n;j++) { // We check for major and minor diagonal and print * for each such cell . if(i==0 || i==n-1 || j==0 || j==n-1 || i == j || j == n-i-1) { System.out.print("* "); } // Otherwise we print blank space. else System.out.print(" "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
Rhombus Star Pattern in java
Given n number of rows, we need to print a Rhombus Star Pattern. Let us look at the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public class PatternExercise { public static void main(String args[]) { int n = 5; //Outer Loop for number of Rows for(int i=0;i<n;i++) { // Print Spaces before each row for(int k=0;k<n-i;k++) { System.out.print(" "); } // printing '*' in each column. for(int j=0;j<n;j++) { System.out.print("* "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 |
* * * * * * * * * * * * * * * * * * * * * * * * * |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public class PatternExercise { public static void main(String args[]) { int n = 5; //Outer Loop for number of Rows for(int i=0;i<n;i++) { // printing '*' in each column. for(int j=0;j<=i;j++) { System.out.print("* "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 |
* * * * * * * * * * * * * * * |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class PatternExercise { public static void main(String args[]) { int n = 6; //Outer Loop for number of Rows for(int i=0;i<n;i++) { //This prints spaces before each row. for(int k=i;k<n;k++) System.out.print(" "); // printing '*' in each column. for(int j=0;j<n;j++) { // We print '*' for only first and last column and for last row. if(j==0 || j==i || i==n-1) System.out.print(" *"); // Otherwise we print Blank Space else System.out.print(" "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 |
* * * * * * * * * * * * * * * |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class PatternExercise { public static void main(String args[]) { int n = 7; // Number of rows. //Outer Loop for number of Rows for(int i=0;i<n;i++) { // printing '*' in each column. for(int j=n;j>i;j--) { // We print '*' for only first and last column and for first row. if(j==n || j==i+1 || i==0) System.out.print("* "); // Otherwise we print Blank Space else System.out.print(" "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 |
* * * * * * * * * * * * * * * * * * |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class PatternExercise { public static void main(String args[]) { int n = 5; // Number of rows. //Outer Loop for number of Rows for(int i=0;i<n;i++) { for(int k=i;k<n;k++) System.out.print(" "); // printing '*' in each column. for(int j=0;j<=i;j++) { // We print '*' for each row. System.out.print("* "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 |
* * * * * * * * * * * * * * * |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class PatternExercise { public static void main(String args[]) { int n = 6; // Number of rows. //Outer Loop for number of Rows for(int i=0;i<n;i++) { for(int k=i;k<n;k++) System.out.print(" "); // We run loop till j = 2*i to print odd no. of stars. for(int j=0;j<=2*i;j++) { // We print '*' for each row. System.out.print("* "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public class PatternExercise { public static void main(String args[]) { int n = 5; // Number of rows. //Outer Loop for number of Rows for(int i=n;i>0;i--) { for(int k=i;k<n;k++) System.out.print(" "); // We run loop from j = 2*i to 2 print odd no. of stars. for(int j=2*i;j>1;j--) { // We print '*' for each row. System.out.print("* "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 |
* * * * * * * * * * * * * * * * * * * * * * * * * |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public class PatternExercise { public static void main(String args[]) { int n = 8; // Number of rows. //Outer Loop for number of Rows for(int i=0;i<n;i++) { for(int k=i;k<n;k++) System.out.print(" "); // We run loop till j = 2*i to print odd no. of stars. for(int j=0;j<=2*i;j++) { // We print '*' for first and last column of each row. // and complete for the last row if(j==0 || j==2*i || i==0 || i==n-1) System.out.print("* "); // Or we print blank space else System.out.print(" "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
public class PatternExercise { public static void main(String args[]) { int n = 5; // Number of rows. // We first print Right Angled Triangle //Outer Loop for number of Rows for(int i=0;i<n;i++) { // printing '*' in each column. for(int j=0;j<=i;j++) { // We print '*' for each row. System.out.print("* "); } System.out.println(); } // Then we print Inverted Right Angled Triangle for(int i=0;i<n-1;i++) { for(int j=i;j<n-1;j++) { System.out.print("* "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
* * * * * * * * * * * * * * * * * * * * * * * * * |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
public class PatternExercise { public static void main(String args[]) { int n = 7; // Number of rows. n= n/2 + 1; int i=0; //We first print Pyramid Pattern for(i=0;i<n;i++) { for(int k=i;k<n;k++) System.out.print(" "); // We run loop till j = 2*i to print odd no. of stars. for(int j=0;j<=2*i;j++) { // We print '*' for each row. System.out.print("* "); } System.out.println(); } n=i; // i=n-1 so copy its value to n. // We then print Inverted Pyramid Traingle. for(i=n-1;i>0;i--) { // We need to print one space for first row. for(int k=i;k<n+1;k++) System.out.print(" "); // We run loop from j = 2*i to 2 print odd no. of stars. for(int j=2*i;j>1;j--) { // We print '*' for each row. System.out.print("* "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 |
* * * * * * * * * * * * * * * * * * * * * * * * * |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
public class PatternExercise { public static void main(String args[]) { int n = 11; // Number of rows. n = n/2 + 1; int i=0; //We first print Upper Part using Right Angled Logic. for(i=0;i<n;i++) { // We avoid space for last row with 1 star for(int k=i;k<n-1;k++) System.out.print(" "); // No. of '*' for each row changes with i. for(int j=i;j<n;j++) { // We print '*' for each row. System.out.print("* "); } System.out.println(); } n=i; // i=n so copy its value to n. // We then print Lower Part of Pattern. // The lower part always start with 2 stars in first row so we set m=2. int m=2; for(i=n;i>1;i--) { // We need to print one space for first row. for(int k=i;k<n+1;k++) System.out.print(" "); // We increment m after this loop to continue printing stars equal to row number. for(int j=0;j<m;j++) { // We print '*' for each row. System.out.print("* "); } m++; System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
public class PatternExercise { public static void main(String args[]) { int n = 9; // Number of rows. n = n/2 + 1; int i=0; //We first print Upper Part using Inverted Right Angled Logic. for(i=0;i<n;i++) { // We avoid space for first row for(int k=0;k<i;k++) System.out.print(" "); // No. of '*' for each row changes with i. for(int j=i;j<n;j++) { // We print '*' for each row. System.out.print("* "); } System.out.println(); } n=i; // i=n so copy its value to n. // We then print Lower Part of Pattern. // The lower part always start with 2 stars in first row so we set m=2. int m=2; for(i=0;i<n-1;i++) { // We avoid blank space for last row. for(int k=i;k<n-2;k++) System.out.print(" "); // We increment m after this loop to continue printing stars equal to row number. for(int j=0;j<m;j++) { // We print '*' for each row. System.out.print("* "); } m++; System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public class PatternExercise { public static void main(String args[]) { int n = 8; // Number of rows. int m = 2*n-1; // So we need to print total 15 rows. // Outer loop for number of rows. for(int i=1;i<=m;i++) { for(int j=1;j<=m;j++) { // for each row we print * for the first and last column. if(i==j || j==(m-i+1)) { System.out.print("*"); } // otherwise we print blank space. else System.out.print(" "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
public class PatternExercise { public static void main(String args[]) { int n = 6; // Number of rows. // Print upper half of pattern for(int i=1; i<=n; i++) { // We print left side stars. for(int j=i; j<=n; j++) { System.out.print("*"); } // Then print spaces , first row has one blank space for(int j=1; j<=(2*i-1); j++) { System.out.print(" "); } // then we print right side stars. for(int j=i; j<=n; j++) { System.out.print("*"); } System.out.println(); } // Loop to print lower half of the pattern for(int i=1; i<=n; i++) { for(int j=1; j<=i; j++) { System.out.print("*"); } // Here we decrease no. of blank spaces after each printing each row. for(int j=(2*i-2); j<(2*n-1); j++) { System.out.print(" "); } for(int j=1; j<=i; j++) { System.out.print("*"); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
****** ****** ***** ***** **** **** *** *** ** ** * * * * ** ** *** *** **** **** ***** ***** ****** ****** |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
class Pattern_ChristmasTree { public static void main(String[] args) { //Value 4 is height of tree or total pyramids to print. int height = 4; //Assigning Width which is width of each pyramid. int width = 4; //Assigning Space // These spaces will be printed before each pyramid. int space = width * height; int x = 1; //Code to Print Upper Part of the Tree i.e. Pyramids. for(int a = 1;a <= height ;a++) { for(int i = x;i <= width;i++) { for(int j = space;j >= i;j--) { System.out.print(" "); } for(int k = 1;k <= i;k++) { System.out.print("* "); } System.out.println(); } // we increase x and width for printing each pyramid. x = x+2; width = width+2; } //Printing Branch or trunk of Christmas Tree // we run loop till i = 3 i.e. height -1. for(int i = 1;i <= height-1;i++) { // we can also initailize j = space - height - 1. for(int j = space-3;j >= 0;j--) { System.out.print(" "); } for(int k= 1;k <= height-1;k++) { System.out.print("* "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
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.