If you have any query feel free to chat us!
Happy Coding! Happy Learning!
In Java, you can create various patterns using loops. Patterns are a great way to practice loop control and create interesting visual designs. Here are a few examples of different patterns you can create using nested loops:
markdownCopy code*
**
***
****
*****
javaCopy codepublic class Pattern1 {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
markdownCopy code*****
****
***
**
*
javaCopy codepublic class Pattern2 {
public static void main(String[] args) {
for (int i = 5; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
markdownCopy code *
***
*****
*******
*********
javaCopy codepublic class Pattern3 {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
markdownCopy code *
***
*****
*******
*********
*******
*****
***
*
javaCopy codepublic class Pattern4 {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
for (int i = n - 1; i >= 1; i--) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
These are just a few examples of the patterns you can create using loops in Java. Patterns can be as simple or as complex as you like, depending on the logic you use in the loops. They are a fun and creative way to practice loop control and problem-solving skills.
Under Section 107 of the copyright act 1976, allowance is made for fair use for purposes such as criticism, comment, news reporting, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use.
Comments: 0