Print Different Star Pattern :
In Java language, You can simply print triangle shape using ‘for loop’ & also using ‘while loop’. Here, I will show you in simple way to print triangle in different shapes.
Pattern 1
Java Code :-
class StarTriangle { public static void main(String[ ] args) { int i,j,k; for(i=1; i<=5; i++) { for(j=4; j>=i; j--) { System.out.print(" "); } for(k=1; k<=(2*i-1); k++) { System.out.print("*"); } System.out.println(""); } } }
Pattern 2
Java Code :-
class Star { public static void main(String[ ] args) { int i, j, k; for(i=5;i>=1;i--) { for(j=5;j>i;j--) { System.out.print(" "); } for(k=1;k<(i*2);k++) { System.out.print("*"); } System.out.println(); } } }
Pattern 3
*
* *
* * *
* * * *
* * * * *
Java Code :-
class Star { public static void main(String[ ] args) { int i,j; for(i=1; i<=6; i++) { for(j=1; j<i; j++) { System.out.print("*"); } System.out.println(); } } }
Pattern 4
* * * * *
* * * *
* * *
* *
*
Java Code :-
class Star { public static void main(String[ ] args) { int i, j; for(i=5;i>=1;i--) { for(j=1;j<=i;j++) { System.out.print("*"); } System.out.println(); } } }
Pattern 5
Java Code :-
class Star { public static void main(String[ ] args) { int i, j, k; for(i=5;igt;=1;i--) { for(j=1;jlt;i;j++) { System.out.print(" "); } for(k=5;k>=i;k--) { System.out.print("*"); } System.out.println(); } } }
Pattern 6
Java Code :-
class Star { public static void main(String[ ] args) { int i, j, k; for(i=5;i>=1;i--) { for(j=5;j>i;j--) { System.out.print(" "); } for(k=1;k<=i;k++) { System.out.print("*"); } System.out.println(); } } }
Pattern 7
Java Code :-
class Star { public static void main(String[ ] args) { int i, j, k; for(i=1;i<=5;i++) { for(j=i;j<5;j++) { System.out.print(" "); } for(k=1;k<(i*2);k++) { System.out.print("*"); } System.out.println(); } for(i=4;i>=1;i--) { for(j=5;j>i;j--) { System.out.print(" "); } for(k=1;k<(i*2);k++) { System.out.print("*"); } System.out.println(); } } }
Syntax To Compile & Run “Java” Program
To Compile -> c:/>javac StarTraingle.java
To Run -> c:/>java StarTraingle