# Pattern – 20: Symmetric-Butterfly Pattern

Problem Statement: Given an integer N, print the following pattern :

Here, N = 5.

Examples:

```Input Format: N = 3
Result:
*    *
**  **
******
**  **
*    *

Input Format: N = 6
Result:
*          *
**        **
***      ***
****    ****
*****  *****
************
*****  *****
****    ****
***      ***
**        **
*          *
```

### Solution

Disclaimer: Don’t jump directly to the solution, try it out yourself first.

Approach

There are 4 general rules for solving a pattern-based question :

• We always use nested loops for printing the patterns. For the outer loop, we count the number of lines/rows and loop for them.
• Next, for the inner loop, we focus on the number of columns and somehow connect them to the rows by forming a logic such that for each row we get the required number of columns to be printed.
• We print the numbers inside the inner loop.
• Observe symmetry in the pattern or check if a pattern is a combination of two or more similar patterns or not.

In this problem, we have to print a butterfly-like star pattern. This pattern is very similar to Pattern 10 in this series as here we can see that for Row 1 we have 2 stars, and 8 spaces, and for Row 2 we have 4 stars and 6 spaces, and so on. Also, after the nth row, the stars decrease and the spaces increase. So, the outer loop will run for 2*n -1 times ( n times when spaces > stars and then n-1 when stars > spaces ). There will be 3 inner loops, one to print stars, then spaces, and then again stars. Spaces will decrement by 2 as i increases and when i reaches n, then spaces decrement by 2 every time we enter a new row. When i<n, the stars printed in each row are 2*i, and as soon as i>n, the stars in each row would be twice of 2*n-i.

Code

## C++ Code

``````#include <bits/stdc++.h>
using namespace std;

void pattern20(int n)
{
// initialising the spaces.
int spaces = 2*n-2;

// Outer loop for printing row.
for(int i = 1;i<=2*n-1;i++){

// stars for first half
int stars = i;

// stars for the second half.
if(i>n) stars = 2*n - i;

//for printing the stars
for(int j=1;j<=stars;j++){
cout<<"*";
}

//for printing the spaces
for(int j = 1;j<=spaces;j++){
cout<<" ";
}

//for printing the stars
for(int j = 1;j<=stars;j++){
cout<<"*";
}

// As soon as the stars for each iteration are printed, we move to the
// next row and give a line break otherwise all stars
// would get printed in 1 line.
cout<<endl;
if(i<n) spaces -=2;
else spaces +=2;
}

}

int main()
{
// Here, we have taken the value of N as 5.
// We can also take input from the user.
int N = 5;

pattern20(N);

return 0;
}
``````

Output

*            *
**         **
***      ***
****   ****
**********
****   ****
***      ***
**         **
*            *

## Java Code

``````class Main {

static void pattern20(int n)
{
// initialising the spaces.
int spaces = 2*n-2;

// Outer loop for printing row.
for(int i = 1;i<=2*n-1;i++){

// stars for first half
int stars = i;

// stars for the second half.
if(i>n) stars = 2*n - i;

//for printing the stars
for(int j=1;j<=stars;j++){
System.out.print("*");
}

//for printing the spaces
for(int j = 1;j<=spaces;j++){
System.out.print(" ");
}

//for printing the stars
for(int j = 1;j<=stars;j++){
System.out.print("*");
}

// As soon as the stars for each iteration are printed, we move to the
// next row and give a line break otherwise all stars
// would get printed in 1 line.
System.out.println();
if(i<n) spaces -=2;
else spaces +=2;
}
}

public static void main(String[] args) {

// Here, we have taken the value of N as 5.
// We can also take input from the user.
int N = 5;
pattern20(N);
}
}
``````

Output

*            *
**         **
***      ***
****   ****
**********
****   ****
***      ***
**         **
*            *