Program to Print Butterfly Pattern (Star Pattern)
Last Updated :
10 Feb, 2024
Given an integer N, print N rows of Butterfly pattern.

Examples:
Input: 3
Output:
* *
** **
*****
** **
* *
Input: 5
Output:
* *
** **
*** ***
**** ****
*********
**** ****
*** ***
** **
* *
Approach:
The problem can be solved using three nested loops inside an outer loop. The outer loop will run for the rows, the first inner loop will print the stars, the second inner loop will print the spaces and the third inner loop will again print the stars.
Step-by-step algorithm:
- Maintain two variable spaces = 2 * N - 1 and stars = 0 to store the number of spaces and stars for each row.
- Run an outer loop from i = 1 to the number of rows (2 * N - 1).
- If we are in the upper half of the butterfly, decrease number of spaces by 2 and increase number of stars by 1.
- If we are in the lower half of the butterfly, increase number of spaces by 2 and decrease number of stars by 1.
- Run an inner loop from j = 1 to stars.
- Print an asterisk in each iteration of the inner loop.
- Run an inner loop from j = 1 to spaces.
- Print a space in each iteration of the inner loop.
- Run an inner loop from j = 1 to stars.
- Print an asterisk in each iteration of the inner loop.
- Print a newline character ("\n") to move to the next row.
- After N iterations, we will have the right half pyramid pattern.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
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 << "\n";
}
return 0;
}
Java
public class ButterflyPattern {
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();
}
}
}
//this code is contributed by Adarsh
Python3
# 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() # Move to the next line
C#
using System;
class Program
{
static void Main()
{
// Number of rows
int N = 5;
// Variables to store the 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();
}
}
}
JavaScript
// Number of rows
const 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
for (let j = 1; j <= stars; j++) {
process.stdout.write("*");
}
// Print spaces
for (let j = 1; j <= spaces; j++) {
process.stdout.write(" ");
}
// Print stars
for (let j = 1; j <= stars; j++) {
if (j !== N) {
process.stdout.write("*");
}
}
process.stdout.write("\n");
}
Output* *
** **
*** ***
**** ****
*********
**** ****
*** ***
** **
* *
Time Complexity: O(N^2), where N is the number of rows in the pattern.
Auxiliary Space: O(1)
Similar Reads
Program to Print Full Pyramid Pattern (Star Pattern) Given an integer N, the task is to print a full pyramid pattern with N rows. In this pattern, each row contains an odd number of stars, ranging from 1 star in the first row to (2 * N - 1) stars in the Nth row. All the stars are center-aligned. Examples: Input: 3Output: * ******** Input: 5Output: * *
4 min read
Program to Print Left Half Pyramid Pattern (Star Pattern) Given an integer N, the task is to print N rows of left half pyramid pattern. In left half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are right aligned. Examples: Input: 3Output: * *****Input: 5Output: * ** *** ****
4 min read
Program to Print Right Half Pyramid Pattern (Star Pattern) Given an integer N, print N rows of right half pyramid pattern. In right half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are left aligned. Examples: Input: 3Output: ****** Input: 5Output: *************** Approach: T
3 min read
Program to print number with star pattern We have to print the pattern as given in the below example.Examples : Input : 5 Output : 1 1*2 1*2*3 1*2 1 Input : 9 Output : 1 1*2 1*2*3 1*2*3*4 1*2*3*4*5 1*2*3*4 1*2*3 1*2 1 C++ #include <iostream> using namespace std; // C++ program to print above pattern void display(int n) { // 'sp' used
6 min read
Program to Print Inverted Left Half Pyramid Pattern (Star Pattern) Given an integer N, the task is is to print a left half pyramid pattern with N rows. In this pattern, the first row contains N stars, the second row contains N - 1 stars, and so forth until the Nth row, which contains only 1 star. All the stars are aligned to the right. Examples: Input: 3Output: ***
4 min read