Computer Science, asked by divyanshsharma1505, 10 months ago

how to make a butterfly pattern in java using for loops

Answers

Answered by usmanihumahu
1

Answer:

package mypack;

public class Loop {

public static void main(String[] args) {

   for(int i=0;i<=8;i++){

       for(int j=1;j<=i+1;j++){

           System.out.print(j);

       }

       System.out.println();

   }

   for(int i=8;i>=1;i--){

       for(int j=1;j<=i;j++){

           System.out.print(j);

       }

       System.out.println();

   }

       }

   }

output::

1

12

123

1234

12345

123456

1234567

12345678

123456789

12345678

1234567

123456

12345

1234

123

12

1

i want the mirror image of it on right side. can someone help me with this.

java

Explanation:

Answered by palwindersaini961
2

Answer:

What you can do is to count the number of spaces before you add the reflection of the numbers to form a butterfly effect in the console, using the String.format

sample:sample:

public static void main(String[] args) {

int j;

for (int i = 0; i <= 8; i++) {

for ( j = 1; j <= i + 1; j++) {

System.out.print(j);

}

System.out.print(String.format("%"+((10 - j)*2 != 0 ? (10 - j)*2 : "")+"s", ""));

for ( j = i + 1; j >= 1; j--) {

System.out.print(j);

}

System.out.println();

}

for (int i = 8; i >= 1; i--) {

for ( j = 1; j <= i ; j++) {

System.out.print(j);

}

System.out.print(String.format("%"+((10 - j)*2 != 0 ? (10 - j)*2 : "")+"s", ""));

for ( j = i; j >= 1; j--) {

System.out.print(j);

}

System.out.println();

}

}

results:

1 1

12 21

123 321

1234 4321

12345 54321

123456 654321

1234567 7654321

12345678 87654321

123456789987654321

12345678 87654321

1234567 7654321

123456 654321

12345 54321

1234 4321

123 321

12 21

1 1

Explanation:

please mark my answer as a brain list and also follow me please like it if it helped you...

Similar questions