Star Pattern Programs in Java
Star Pattern Programs in Java
The star pattern programs in Java is the part of pattern programs in Java, which we discussed earlier.
Right Triangle Star Pattern
Filename: StarPatternExample.java
public class StarPatternExample
{
public static void main(String argvs[])
{
int row = 7; // numer of rows to be printed
int r, c;
for(r = 0; r < row; r++) //loop for rows
{
for(c = 0; c <= r; c++) // loop for columns
{
System.out.print("* "); // prints stars
}
System.out.println(); // at the end of each row throws the cursor to the next line
}
}
}
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Mirrored Right Triangle Star Pattern
Filename: StarPatternExample2.java
public class StarPatternExample2
{
public static void main(String argvs[])
{
int r, c;
int row = 7; //number of rows to be printed
for (r = 0; r < row; r++) //loop for rows
{
for (c = r; c < row; c++) //loop for columns
{
System.out.print("* "); // Printing stars
}
System.out.println(); // at the end of each row throws the cursor to the next line
}
}
}
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Mirrored Left Triangle Star Pattern
Filename: StarPatternExample3.java
public class StarPatternExample3
{
public static void main(String argvs[])
{
int row = 7; //number of rows to be printed
int r, c;
for(r = 0; r < row; r++) //loop for rows
{
for(int ind = 0; ind < 2 * r + 1; ind++) // This loop handles indentation
{
System.out.print(" "); // prints spaces
}
for(c = r; c < row; c++) // loop for columns
{
System.out.print("* "); // Displaying stars
}
System.out.println(); // at the end of each row throws the cursor to the next line
}
}
}
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Triangle Star Pattern
Filename: StarPatternExample4.java
public class StarPatternExample4
{
public static void main(String argvs[])
{
int rows = 7; // number of rows to be printed
for (int r = 1; r <= rows; r++) // loop for rows
{
for (int ind = r; ind < rows; ind++) // Handling indentation for each row
{
System.out.print(" "); // Printing spaces
}
for (int c = 1; c <= (2*r -1); c++) //loop for columns
{
// Condition for displaying stars
if( c == 1 || r == rows || c == (2*r - 1))
{
System.out.print("*");
}
else // Handling inter-star gaps
{
System.out.print(" ");
}
}
System.out.println(); // at the end of each row throws the cursor to the next line
}
}
}
Output:
* * * * * * * * * * * *************
Down Triangle Star Pattern
Filename: StarPatternExample5.java
public class StarPatternExample5
{
public static void main(String[] args)
{
int rows = 7; // number of rows to be printed
for (int r = rows; r >= 1; r--) // loop for rows
{
for (int ind = r; ind < rows ; ind++) // Handling indentation for each row
{
System.out.print(" ");
}
for (int c = 1; c <= (2*r -1); c++) //loop for columns
{
// Conditions for printing stars
if( c ==1 || r == rows || c ==(2*r - 1))
{
System.out.print("*");
}
else // Dealing with inter-star gaps
{
System.out.print(" ");
}
}
System.out.println(); // at the end of each row throws the cursor to the next line
}
}
}
Output:
************* * * * * * * * * * * *
Diamond Star Pattern
Filename: StarPatternExample6.java
public class StarPatternExample6
{
public static void main(String argvs[])
{
int rows = 8; // number of rows to be printed
//logic to print the first half of the pattern
for (int r = 1; r <= rows; r++) // Handling rows
{
for (int ind = rows; ind > r; ind--) // Handling indentation for each row
{
System.out.print(" ");
}
System.out.print("*"); // Printing stars
for (int sp = 1; sp < 2*(r -1); sp++) // Handling inter-star spaces
{
System.out.print(" "); // Printing spaces
}
if(r == 1) // Only one star for the first row
{
System.out.println("");
}
else // Every row contains 2 stars barring the first and last row.
{
System.out.println("*");
}
}
// logic to print the second half of the pattern
for (int r = rows-1; r >= 1; r--) // loop for rows
{
for (int ind = rows; ind > r ; ind--) // Handling indentation
{
System.out.print(" ");
}
System.out.print("*"); // Printing star
for (int sp = 1; sp < 2*(r - 1); sp++) // Handling inter-star spaces
{
System.out.print(" ");
}
if(r == 1) // Only one star for the last row
System.out.println("");
else // Every row contains 2 stars barring the first and last row.
System.out.println("*");
}
}
}
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
Star Pattern – K Shaped
Filename: StarPatternExample7.java
public class StarPatternExample7
{
public static void main(String argvs[])
{
int row = 9; //number of rows to be printed
// logic to print the first half of the k-shaped pattern
for(int r = 0; r < row; r++)
{
// loop for columns
for(int c = 0; c < row - r; c++)
{
System.out.print("* "); // Printing stars
}
System.out.println(); // moves the cursor to the next line
}
// logic to print the second half of the k-shaped pattern
for(int r = 1; r < row; r++)
{
for(int c = 0; c <= r; c++)
{
System.out.print("* "); // Printing stars
}
System.out.println(); // moves the cursor to the next line
}
}
}
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Star Pattern – Hour-Glass Shaped
Filename: StarPatternExample8.java
public class StarPatternExample8
{
public static void main(String argvs[])
{
int rows = 9; // number of rows to be printed
//logic to print the first half of the row
for (int r= 0; r <= rows-1; r++) // loop for rows
{
for (int ind = 0; ind < r; ind++) // Dealing with indentation
{
System.out.print(" ");
}
for (int st=r; st<=rows-1; st++) // Loop for printing stars and spaces
{
System.out.print("*" + " ");
}
// This statement induces line break at the end of each row
System.out.println("");
}
//logic to print second half of the pattern
for (int r= rows-1; r >= 0; r--)
{
for (int ind = 0; ind < r; ind++) // Dealing with indentation
{
System.out.print(" ");
}
for (int st = r; st <= rows-1; st++) // Loop for printing stars
{
System.out.print("*" + " ");
}
// This statement induces line break at the end of each row
System.out.println("");
}
}
}
Output:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Star Pattern – Alphabet A Shaped
Filename: StarPatternExample9.java
public class StarPatternExample9
{
public static void main(String argvs[])
{
int rows = 9; // number of rows to be printed
for (int r = 0; r <= rows; r++) // loop for rows
{
for (int j = 0; j <= rows / 2; j++)
{
// prints the two vertical lines of the alphabet A
if ((j == 0 || j == rows / 2) && r != 0 ||
// Displays the top most line of the alphabet A
r == 0 && j != rows / 2 ||
// printing the middle line
r == rows / 2)
System.out.print("*"); // Printing stars
else
System.out.print(" "); // Printing inter-star spaces
}
System.out.println(); // moves cursor to the next line
}
}
}
Output:
**** * * * * * * ***** * * * * * * * *
