![]() |
![]() |
This tutorial demonstrates about how to print interesting star pattern programs in java language.
Star patterns are the shapes drawn on the console window using asterisk or star (*) symbol.
For this we have to use different nested loops to repeatedly draw the stars on the screen.
public class StarPatternEx { public static void main(String[] args) { int rows = 4; for(int i=1;i<=rows;i++) // single row { for(int j=1;j<=i;j++) // repeat in rows { System.out.print("*"); // print stars } System.out.print("\n"); // move to new line } } }
public class StarPatternEx { public static void main(String[] args) { for(int i=1;i<=4;i++) // single row { for(int j=i;j<=4;j++) // repeat in rows { System.out.print("*"); // print stars } System.out.print("\n"); // move to new line } } }
public class StarPatternEx { public static void main(String[] args) { int firstRowStars = 1; int rows = 3; int maxSpaces=rows-1; for(int i=0;i<rows;i++) { for(int j=maxSpaces;j>i;j--) { System.out.print(" "); } for(int k=0;k<firstRowStars;k++) { System.out.print("*"); } firstRowStars += 2; System.out.print("\n"); } } }
* *** *****
public class StarPatternEx { public static void main(String[] args) { int firstRowStars = 5; int rows = firstRowStars /2; int maxSpaces=firstRowStars-rows; for(int i=maxSpaces;i>=1;i--) { for(int j=maxSpaces;j>=i;j--) { System.out.print(" "); } for(int k=1;k<=firstRowStars;k++) { System.out.print("*"); } firstRowStars -= 2; System.out.print("\n"); } } }
***** *** *
public class StarPatternEx { public static void main(String[] args) { int rows = 4; int columns = 4; for(int i=1;i<=rows;i++) { for(int j=1;j<=columns;j++) { System.out.print("*"); } System.out.print("\n"); } } }
**** **** **** ****