Solving interesting pattern problems can enhance understanding of loops. They are essential because they help build a solid foundation in a specific programming language. There are various modes, including number-based, asterisk-based, and letter-based modes. In this article, we will discuss some Java programs to print interesting star patterns.
Declare and initialize an integer "n" that specifies the number of rows and columns.
Define a for loop that will run to "n". Define an if-else block inside this loop.
The if block will print the asterisk "n" times in the first and last lines, the else block will print the space "n/2" in the second to fourth lines, which means 2 times and the asterisk No. once.
public class P1 { public static void main(String[] args) { int n = 5; for(int i = 1; i <= n; i++) { // till number of rows if(i == 1 || i == n) { for(int str = 1; str <= n; str++) { System.out.print("*\t"); } } else { for(int spc = 1; spc <= n/2; spc++) { System.out.print("\t"); } for(int str = 1; str <= 1; str++){ System.out.print("*"); } } System.out.println(); // to move cursor to next line } } }
* * * * * * * * * * * * *
Declares and initializes an integer "n" specifying the number of rows.
Create a nested for loop, the outer loop will run to 'n', the inner loop will run to the number of spaces and print the spaces. After printing, we decrement the space count by 1.
Again takes another inner for loop that will run to the number of stars and print the stars. After printing, we increase the star count by 2.
public class P2 { public static void main(String[] args) { int n = 5; int spc = n-1; // initial space count int str = 1; // initial star count for(int i = 1; i <= n; i++) { for(int j = 1; j <= spc; j++) { // spaces System.out.print("\t"); } spc--; for(int k = 1; k <= str; k++) { // stars System.out.print("*\t"); } str += 2; System.out.println(); } } }
* * * * * * * * * * * * * * * * * * * * * * * * *
Declares and initializes an integer "n" specifying the number of rows.
Define a nested for loop, the outer loop will run from 'n' to 1, the first inner loop will print spaces, and the second inner loop will print asterisks.
public class P3 { public static void main(String[] args) { int n=5; for(int i = n; i >= 1; i--) { for(int spc = n-i; spc >= 1; spc--){ // spaces System.out.print("\t"); } for(int str = 1; str <= i; str++){ // stars System.out.print("*\t"); } System.out.println(); } } }
* * * * * * * * * * * * * * *
Declares and initializes an integer "n" specifying the number of rows.
Create a nested for loop, the outer loop will run to "n", the first inner for loop will run to the number of spaces and print the spaces. The second one will print stars.
Now, using the if-else block, the if block will check if the line number is less than 3. If less than then execute the if block to decrement the space count and increment the star count.
If the line number is greater than 2, execute the else block to increment the space count and decrement the asterisk count.
public class P4 { public static void main(String[] args) { int n = 5; int spc = 2; // initial space count int str = 1; // initial star count for (int i = 1; i <= n; i++) { for (int j = 1; j <= spc; j++) { // spaces System.out.print("\t"); } for (int j = 1; j <= str; j++) { // stars System.out.print("*\t"); } if ( i <= 2) { spc--; str += 2; } else { spc++; str -= 2; } System.out.println(); } } }
* * * * * * * * * * * * *
Declares and initializes an integer "n" specifying the number of rows.
Define nested for loops, the outer loop will run to "n", the first inner loop will print spaces, and the second inner loop will print stars.
public class P5 { public static void main(String[] args) { int n = 5; for(int i = 1; i <= n; i++){ for(int spc = 1; spc <= n-i; spc++) { System.out.print("\t"); } for(int str = 1; str <= i; str++) { System.out.print("*\t"); } System.out.println(); } } }
* * * * * * * * * * * * * * *
In this article, we discuss the interesting problem of star patterns. These pattern solutions will help you decode the logic of pattern problems and enable you to solve other patterns on your own.
The above is the detailed content of Program to print interesting patterns. For more information, please follow other related articles on the PHP Chinese website!