Given an integer N, print a butterfly star pattern with 2N − 1 rows. The number of stars increases from 1 to N in the upper half and then decreases from N − 1 to 1 in the lower half, forming a symmetric butterfly shape.

Examples:
Input: 3
Output:
* *
** **
*****
** **
* *Input: 5
Output:
* *
** **
*** ***
**** ****
*********
**** ****
*** ***
** **
* *
Try it on GfG Practice
Using Nested Loops – O(N²) Time and O(1) Space
The butterfly pattern can be printed using nested loops. The outer loop runs for all rows, while three inner loops are used to print the left stars, spaces, and right stars. In the upper half, the number of stars increases and spaces decrease. In the lower half, the stars decrease and spaces increase, forming the butterfly shape.
Step-wise Approach
- Initialize two variables spaces = 2 * N - 1 and stars = 0 to track the number of spaces and stars in each row.
- Run an outer loop from i = 1 to 2 * N - 1 to generate all rows of the butterfly pattern.
- If the current row is in the upper half (i ≤ N), decrease spaces by 2 and increase stars by 1.
- If the row is in the lower half (i > N), increase spaces by 2 and decrease stars by 1.
- Print stars number of * characters using a loop.
- Print spaces number of spaces using another loop.
- Print stars number of * characters again to form the right wing of the butterfly.
- Print a newline character to move to the next row.
//Driver Code Starts
using namespace std;
//Driver Code Ends
int main()
{
// Number of rows
int N = 5;
// Variables to store number of spaces and stars
int spaces = 2 * N - 1;
int stars = 0;
// The outer loop will run for (2 * N - 1) times
for (int i = 1; i <= 2 * N - 1; i++) {
// Upper half of the butterfly
if (i <= N) {
spaces = spaces - 2;
stars++;
}
// Lower half of the butterfly
else {
spaces = spaces + 2;
stars--;
}
// Print stars
for (int j = 1; j <= stars; j++) {
cout << "*";
}
// Print spaces
for (int j = 1; j <= spaces; j++) {
cout << " ";
}
// Print stars
for (int j = 1; j <= stars; j++) {
if (j != N) {
cout << "*";
}
}
cout << "
";
}
return 0;
}
//Driver Code Starts
class GFG {
//Driver Code Ends
public static void main(String[] args)
{
// Number of rows
int n = 5;
// Variables to store number of spaces and stars
int spaces = 2 * n - 1;
int stars = 0;
// The outer loop will run for (2 * n - 1) times
for (int i = 1; i <= 2 * n - 1; i++) {
// Upper half of the butterfly
if (i <= n) {
spaces = spaces - 2;
stars++;
}
// Lower half of the butterfly
else {
spaces = spaces + 2;
stars--;
}
// Print stars
for (int j = 1; j <= stars; j++) {
System.out.print("*");
}
// Print spaces
for (int j = 1; j <= spaces; j++) {
System.out.print(" ");
}
// Print stars
for (int j = 1; j <= stars; j++) {
if (j != n) {
System.out.print("*");
}
}
System.out.println();
}
}
}
def main():
# Number of rows
n = 5
# Variables to store number of spaces and stars
spaces = 2 * n - 1
stars = 0
# The outer loop will run for (2 * n - 1) times
for i in range(1, 2 * n):
# Upper half of the butterfly
if i <= n:
spaces = spaces - 2
stars += 1
# Lower half of the butterfly
else:
spaces = spaces + 2
stars -= 1
# Print stars
for j in range(1, stars + 1):
print("*", end="")
# Print spaces
for j in range(1, spaces + 1):
print(" ", end="")
# Print stars
for j in range(1, stars + 1):
if j != n:
print("*", end="")
print()
#Driver Code Starts
if __name__ == "__main__":
main()
#Driver Code Ends
//Driver Code Starts
using System;
class GFG
{
//Driver Code Ends
static void Main()
{
// Number of rows
int n = 5;
// Variables to store number of spaces and stars
int spaces = 2 * n - 1;
int stars = 0;
// The outer loop will run for (2 * n - 1) times
for (int i = 1; i <= 2 * n - 1; i++)
{
// Upper half of the butterfly
if (i <= n)
{
spaces = spaces - 2;
stars++;
}
// Lower half of the butterfly
else
{
spaces = spaces + 2;
stars--;
}
// Print stars
for (int j = 1; j <= stars; j++)
{
Console.Write("*");
}
// Print spaces
for (int j = 1; j <= spaces; j++)
{
Console.Write(" ");
}
// Print stars
for (int j = 1; j <= stars; j++)
{
if (j != n)
{
Console.Write("*");
}
}
Console.WriteLine();
}
}
}
function main()
{
// Number of rows
let n = 5;
// Variables to store number of spaces and stars
let spaces = 2 * n - 1;
let stars = 0;
// The outer loop will run for (2 * n - 1) times
for (let i = 1; i <= 2 * n - 1; i++) {
// Upper half of the butterfly
if (i <= n) {
spaces = spaces - 2;
stars++;
}
// Lower half of the butterfly
else {
spaces = spaces + 2;
stars--;
}
// Print stars
let line = "";
for (let j = 1; j <= stars; j++) {
line += "*";
}
// Print spaces
for (let j = 1; j <= spaces; j++) {
line += " ";
}
// Print stars
for (let j = 1; j <= stars; j++) {
if (j != n) {
line += "*";
}
}
console.log(line);
}
}
//Driver Code Starts
// driver code
main();
//Driver Code Ends