If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Here's an example of how to create a square pattern using loops in Java:
markdownCopy code
*****
*****
*****
*****
*****
javaCopy code
public class SquarePattern {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
In the program above, we use two nested loops to create the square pattern. The outer loop runs n
(5 in this case) times, representing the number of rows, and the inner loop prints the asterisks n
times, representing the number of asterisks in each row. The result is a square pattern with n
rows and n
columns of asterisks.
You can adjust the value of n
to create square patterns of different sizes.
Comments: 0