In this post, you will get to see Advance Level Star Pattern Programs In Java. In the previous post, I have solved the examples of basic star patterns in java. If you haven't seen that post before, check it out at the link below.
But before learning star patterns, you need to know about the loop, nested loop and if-else statements. If you don't know about it, you can learn it easily from the following link.
We are going to do all the programs dry run. So you will notice the flow of code. You need to download the dry run excel file from the link given below the program.
Three types of patterns are printed in the JAVA programming language.
- Star Pattern
- Number Pattern
- Character Pattern
Output:
public class PatternPrograms{
public static void main(String[] args){ int rows = 5; for (int i= 0; i<= rows-1 ; i++){ for (int j=0; j <i; j++){ System.out.print(" "); } for (int k=i; k<=rows-1; k++) { System.out.print("*" + " "); } System.out.println(); } for (int i= rows-1; i>= 0; i--){ for (int j=0; j< i ;j++){ System.out.print(" "); } for (int k=i; k<=rows-1; k++){ System.out.print("*" + " "); } System.out.println(); } } }
Output:
public class DiamondDemo { public static void main(String args[]) { int i, j, space = 1; int rows = 4; space = rows - 1; for (j = 1; j <= rows; j++) { for (i = 1; i <= space; i++) { System.out.print(" "); } space--; for (i = 1; i <= 2 * j - 1; i++) { System.out.print("*"); } System.out.println(""); } space = 1; for (j = 1; j <= rows - 1; j++) { for (i = 1; i <= space; i++) { System.out.print(" "); } space++; for (i = 1; i <= 2 * (rows - j) - 1; i++) { System.out.print("*"); } System.out.println(""); } } }
Output:
public class TechandFun { public static void main(String[] args) { int rows = 4; for (int i= 0; i<= rows-1 ; i++) { for (int j=0; j<=i; j++) { System.out.print("*"+ " "); } System.out.println(""); } for (int i=rows-1; i>=0; i--) { for(int j=0; j <= i-1;j++) { System.out.print("*"+ " "); } System.out.println(""); } } }
Output:
public class TechandFun { public static void main(String[] args) { int rows = 5; for (int i= 1; i<= rows ; i++) { for (int j=i; j <rows ;j++) { System.out.print(" "); } for (int k=1; k<=i;k++) { System.out.print("*"); } System.out.println(""); } for (int i=rows; i>=1; i--) { for(int j=i; j<=rows;j++) { System.out.print(" "); } for(int k=1; k<i ;k++) { System.out.print("*"); } System.out.println(""); } } }
Output:
public class TechandFun{ public static void main(String[] args){ int rows = 5; for (int i=rows; i>= 1 ; i--){ for (int j = i; j < rows ; j++) { System.out.print(" "); } for(int k = 1; k <= (2*i -1) ;k++) { if( k==1 || i == rows || k==(2*i-1)) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(""); } } }
JAVA Star Pattern Programs Examples for Practice
Solve the following Java Star Pattern programs on your skills. And if not, the solutions will be found in the next post.
1. Hollow Square Star Pattern in java.
* * * * * * * * * * * * * * * *
2. Rhombus Star Pattern in java
If there are 5 comments on this post, then the solution of examples will be given. Make quick comments for the solution.
* * * * * * * * * * * * * * * * * * * * * * * * *
Post a Comment