Display Butterfly Pattern
The butterfly pattern is a geometric pattern that resembles the wings of a butterfly. It is created by printing a combination of spaces and symbols in a specific arrangement. This pattern is often used for decorative purposes in various applications.
Problem Statement:

Code Solution
In this program, we aim to display a butterfly pattern based on the given size. The pattern consists of two halves, the top half and the bottom half, which are mirror images of each other. Each half is formed by a combination of spaces and symbols. The size of the pattern determines the number of rows and the complexity of the pattern.
For example, let's consider the butterfly pattern with a size of 4:
⌖ ⌖ ⌖⌖ ⌖⌖ ⌖⁖⌖ ⌖⁖⌖ ⌖⁖⁖⌖⌖⁖⁖⌖ ⌖⁖⌖ ⌖⁖⌖ ⌖⌖ ⌖⌖ ⌖ ⌖
Explanation:
In the above example, the pattern is created by printing spaces and symbols in a specific arrangement. Each row of the pattern consists of three parts: spaces, symbols, and additional spaces.
The top half of the pattern is formed by iterating from 0 to size-1. In each iteration, we print spaces before the symbol, followed by the symbol, then additional spaces, and finally the symbol again. The number of spaces and symbols in each row is determined by the current iteration.
The bottom half of the pattern is formed by iterating from 0 to size-1. In each iteration, we print spaces before the symbol, followed by the symbol, then additional spaces, and finally the symbol again. The number of spaces and symbols in each row is determined by subtracting the current iteration from the size.
Algorithm:
1. Start the program.
2. Define the functions for printing spaces and symbols.
3. Implement the butterfly_pattern function that takes the size as a parameter.
4. Inside the butterfly_pattern function, iterate i from 0 to size-1 for the top half:
a. Call the space function with the current value of i to print spaces before the symbol.
b. Call the print_symbol function with the current value of i to print the symbol.
c. Call the space function with (size*4) - (i*4) to print additional spaces.
d. Call the print_symbol function with the current value of i to print the symbol again.
e. Print a new line.
5. Inside the butterfly_pattern function, iterate i from 0 to size-1 for the bottom half:
a. Call the space function with (size-i) to print spaces before the symbol.
b. Call the print_symbol function with (size-i) to print the symbol.
c. Call the space function with (i*4) to print additional spaces.
d. Call the print_symbol function with (size-i) to print the symbol again.
e. Print a new line.
6. Implement the main function:
a. Call the butterfly_pattern function with different sizes as test cases.
7. End the program.
Pseudocode:
space(size): for counter from 0 to size: print a space print_symbol(size): for counter from 0 to size: if counter equals 0 or counter+1 equals size: print "⌖" else: print "⁖" butterfly_pattern(size): print "Size: " + size for i from 0 to size-1: space(i) print_symbol(i) space((size*4) - (i*4)) print_symbol(i) print a new line for i from 0 to size-1: space(size - i) print_symbol(size - i) space(i * 4) print_symbol(size - i) print a new line main(): butterfly_pattern(4) butterfly_pattern(5) butterfly_pattern(7)
//C Program
//Display Butterfly Pattern
#include <stdio.h>
#include <stdlib.h>
/*Include Space of given size*/
void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
printf(" ");
}
}
/*Include Symbol*/
void print_symbol(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
if (counter == 0 || counter + 1 == size) {
printf("⌖");
} else {
printf("⁖");
}
}
}
void butterfly_pattern(int size) {
printf("\n Size : %d \n", size);
int i = 0;
//Display the result of top half shell
for (i = 0; i < size; i++) {
space(i);
print_symbol(i);
space((size *4) - (i *4));
print_symbol(i);
printf("\n");
}
//Display the result of bottom half shell
for (i = 0; i < size; i++) {
space(size - i);
print_symbol(size - i);
space((i *4));
print_symbol(size - i);
printf("\n");
}
}
int main() {
//Test Cases
butterfly_pattern(4);
butterfly_pattern(5);
butterfly_pattern(7);
return 0;
}
Output
Size : 4
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 5
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 7
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⁖⌖⌖⁖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
/*
C++ Program
Display Butterfly Pattern
*/
#include<iostream>
using namespace std;
class MyPattern {
public:
/*Include Space of given size*/
void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
cout << " ";
}
}
/*Include Symbol*/
void print_symbol(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
if (counter == 0 ||
counter + 1 == size) {
cout << "⌖";
} else {
cout << "⁖";
}
}
}
void butterfly_pattern(int size) {
cout << "\n Size : " << size << " \n";
int i = 0;
//Display the result of top half shell
for (i = 0; i < size; i++) {
this->space(i);
this->print_symbol(i);
this->space((size *4) - (i *4));
this->print_symbol(i);
cout << "\n";
}
//Display the result of bottom half shell
for (i = 0; i < size; i++) {
this->space(size - i);
this->print_symbol(size - i);
this->space((i *4));
this->print_symbol(size - i);
cout << "\n";
}
}
};
int main() {
MyPattern obj = MyPattern();
obj.butterfly_pattern(4);
obj.butterfly_pattern(5);
obj.butterfly_pattern(7);
return 0;
}
Output
Size : 4
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 5
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 7
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⁖⌖⌖⁖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
/*
Java Program
Display Butterfly Pattern
*/
public class MyPattern {
/*Include Space of given size*/
public void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
System.out.print(" ");
}
}
/*Include Symbol*/
public void print_symbol(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
if (counter == 0 || counter + 1 == size) {
System.out.print("⌖");
} else {
System.out.print("⁖");
}
}
}
public void butterfly_pattern(int size) {
System.out.print("\n Size : " + size + " \n" );
int i = 0;
//Display the result of top half shell
for (i = 0; i < size; i++) {
space(i);
print_symbol(i);
space((size *4) - (i *4));
print_symbol(i);
System.out.print("\n");
}
//Display the result of bottom half shell
for (i = 0; i < size; i++) {
space(size - i);
print_symbol(size - i);
space((i *4));
print_symbol(size - i);
System.out.print("\n");
}
}
public static void main(String[] args) {
MyPattern obj = new MyPattern();
obj.butterfly_pattern(4);
obj.butterfly_pattern(5);
obj.butterfly_pattern(7);
}
}
Output
Size : 4
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 5
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 7
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⁖⌖⌖⁖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
/*
C# Program
Display Butterfly Pattern
*/
using System;
public class MyPattern {
/*Include Space of given size*/
public void space(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
Console.Write(" ");
}
}
/*Include Symbol*/
public void print_symbol(int size) {
int counter = 0;
for (counter = 0; counter < size; counter++) {
if (counter == 0 ||
counter + 1 == size) {
Console.Write("⌖");
} else {
Console.Write("⁖");
}
}
}
public void butterfly_pattern(int size) {
Console.Write("\n Size : " + size + " \n");
int i = 0;
//Display the result of top half shell
for (i = 0; i < size; i++) {
space(i);
print_symbol(i);
space((size * 4) - (i * 4));
print_symbol(i);
Console.Write("\n");
}
//Display the result of bottom half shell
for (i = 0; i < size; i++) {
space(size - i);
print_symbol(size - i);
space((i * 4));
print_symbol(size - i);
Console.Write("\n");
}
}
public static void Main(String[] args) {
MyPattern obj = new MyPattern();
obj.butterfly_pattern(4);
obj.butterfly_pattern(5);
obj.butterfly_pattern(7);
}
}
Output
Size : 4
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 5
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 7
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⁖⌖⌖⁖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
<?php
/*
Php Program
Display Butterfly Pattern
*/
class MyPattern {
/*Include Space of given size*/
public function space($size) {
$counter = 0;
for ($counter = 0; $counter < $size; $counter++) {
//Add space
echo(" ");
}
}
/*Include Symbol*/
public function print_symbol($size) {
$counter = 0;
for ($counter = 0; $counter < $size; $counter++) {
if ($counter == 0 ||
$counter + 1 == $size) {
echo("⌖");
} else {
echo("⁖");
}
}
}
public function butterfly_pattern($size) {
echo("\n Size : ". $size ." \n");
$i = 0;
//Display the result of top half shell
for ($i = 0; $i < $size; $i++) {
$this->space($i);
$this->print_symbol($i);
$this->space(($size *4) - ($i *4));
$this->print_symbol($i);
echo("\n");
}
//Display the result of bottom half shell
for ($i = 0; $i < $size; $i++) {
$this->space($size - $i);
$this->print_symbol($size - $i);
$this->space(($i *4));
$this->print_symbol($size - $i);
echo("\n");
}
}
}
function main() {
$obj = new MyPattern();
$obj->butterfly_pattern(4);
$obj->butterfly_pattern(5);
$obj->butterfly_pattern(7);
}
main();
Output
Size : 4
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 5
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 7
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⁖⌖⌖⁖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
/*
Node Js Program
Display Butterfly Pattern
*/
class MyPattern {
/*Include Space of given size*/
space(size) {
var counter = 0;
for (counter = 0; counter < size; counter++) {
//Add space
process.stdout.write(" ");
}
}
/*Include Symbol*/
print_symbol(size) {
var counter = 0;
for (counter = 0; counter < size; counter++) {
if (counter == 0 ||
counter + 1 == size) {
process.stdout.write("⌖");
} else {
process.stdout.write("⁖");
}
}
}
butterfly_pattern(size) {
process.stdout.write("\n Size : " + size + " \n");
var i = 0;
//Display the result of top half shell
for (i = 0; i < size; i++) {
this.space(i);
this.print_symbol(i);
this.space((size *4) - (i *4));
this.print_symbol(i);
process.stdout.write("\n");
}
//Display the result of bottom half shell
for (i = 0; i < size; i++) {
this.space(size - i);
this.print_symbol(size - i);
this.space((i *4));
this.print_symbol(size - i);
process.stdout.write("\n");
}
}
}
function main(args) {
var obj = new MyPattern();
obj.butterfly_pattern(4);
obj.butterfly_pattern(5);
obj.butterfly_pattern(7);
}
main();
Output
Size : 4
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 5
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 7
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⁖⌖⌖⁖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
# Python 3 Program
# Display Butterfly Pattern
class MyPattern :
# Include Space of given size
def space(self, size) :
counter = 0
while (counter < size) :
print(" ", end = "")
counter += 1
# Include Symbol
def print_symbol(self, size) :
counter = 0
while (counter < size) :
if (counter == 0 or counter + 1 == size) :
print("⌖", end = "")
else :
print("⁖", end = "")
counter += 1
def butterfly_pattern(self, size) :
print("\n Size : ", size ," \n", end = "")
i = 0
# Display the result of top half shell
while (i < size) :
self.space(i)
self.print_symbol(i)
self.space((size * 4) - (i * 4))
self.print_symbol(i)
print("\n", end = "")
i += 1
i = 0
# Display the result of bottom half shell
while (i < size) :
self.space(size - i)
self.print_symbol(size - i)
self.space((i * 4))
self.print_symbol(size - i)
print("\n", end = "")
i += 1
def main() :
obj = MyPattern()
obj.butterfly_pattern(4)
obj.butterfly_pattern(5)
obj.butterfly_pattern(7)
if __name__ == "__main__":
main()
Output
Size : 4
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 5
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 7
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⁖⌖⌖⁖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
# Ruby Program
# Display Butterfly Pattern
class MyPattern
# Include Space of given size
def space(size)
counter = 0
while (counter < size)
print(" ")
counter += 1
end
end
# Include Symbol
def print_symbol(size)
counter = 0
while (counter < size)
if (counter == 0 ||
counter + 1 == size)
print("⌖")
else
print("⁖")
end
counter += 1
end
end
def butterfly_pattern(size)
print("\n Size :", size ," \n")
i = 0
# Display the result of top half shell
while (i < size)
self.space(i)
self.print_symbol(i)
self.space((size * 4) - (i * 4))
self.print_symbol(i)
print("\n")
i += 1
end
i = 0
# Display the result of bottom half shell
while (i < size)
self.space(size - i)
self.print_symbol(size - i)
self.space((i * 4))
self.print_symbol(size - i)
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
obj.butterfly_pattern(4)
obj.butterfly_pattern(5)
obj.butterfly_pattern(7)
end
main()
Output
Size :4
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size :5
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size :7
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⁖⌖⌖⁖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
/*
Scala Program
Display Butterfly Pattern
*/
class MyPattern {
/*Include Space of given size*/
def space(size: Int): Unit = {
var counter: Int = 0;
while (counter < size) {
print(" ");
counter += 1;
}
}
/*Include Symbol*/
def print_symbol(size: Int): Unit = {
var counter: Int = 0;
while (counter < size) {
if (counter == 0 ||
counter + 1 == size) {
print("⌖");
} else {
print("⁖");
}
counter += 1;
}
}
def butterfly_pattern(size: Int): Unit = {
print("\n Size : " + size + " \n");
var i: Int = 0;
//Display the result of top half shell
while (i < size) {
this.space(i);
this.print_symbol(i);
this.space((size * 4) - (i * 4));
this.print_symbol(i);
print("\n");
i += 1;
}
i = 0;
//Display the result of bottom half shell
while (i < size) {
this.space(size - i);
this.print_symbol(size - i);
this.space((i * 4));
this.print_symbol(size - i);
print("\n");
i += 1;
}
}
}
object Main {
def main(args: Array[String]): Unit = {
val obj: MyPattern = new MyPattern();
obj.butterfly_pattern(4);
obj.butterfly_pattern(5);
obj.butterfly_pattern(7);
}
}
Output
Size : 4
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 5
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 7
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⁖⌖⌖⁖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
/*
Swift Program
Display Butterfly Pattern
*/
class MyPattern {
/*Include Space of given size*/
func space(_ size: Int) {
var counter = 0;
while (counter < size) {
print(" ", terminator: "");
counter += 1;
}
}
/*Include Symbol*/
func print_symbol(_ size: Int) {
var counter = 0;
while (counter < size) {
if (counter == 0 ||
counter + 1 == size) {
print("⌖", terminator: "");
} else {
print("⁖", terminator: "");
}
counter += 1;
}
}
func butterfly_pattern(_ size: Int) {
print("\n Size : ", size ," \n", terminator: "");
var i = 0;
//Display the result of top half shell
while (i < size) {
self.space(i);
self.print_symbol(i);
self.space((size * 4) - (i * 4));
self.print_symbol(i);
print("\n", terminator: "");
i += 1;
}
i = 0;
//Display the result of bottom half shell
while (i < size) {
self.space(size - i);
self.print_symbol(size - i);
self.space((i * 4));
self.print_symbol(size - i);
print("\n", terminator: "");
i += 1;
}
}
}
func main() {
let obj = MyPattern();
obj.butterfly_pattern(4);
obj.butterfly_pattern(5);
obj.butterfly_pattern(7);
}
main();
Output
Size : 4
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 5
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Size : 7
⌖ ⌖
⌖⌖ ⌖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⁖⌖⌖⁖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⁖⌖ ⌖⁖⁖⁖⁖⌖
⌖⁖⁖⁖⌖ ⌖⁖⁖⁖⌖
⌖⁖⁖⌖ ⌖⁖⁖⌖
⌖⁖⌖ ⌖⁖⌖
⌖⌖ ⌖⌖
⌖ ⌖
Output Explanation:
The output of the program displays the butterfly pattern for different sizes. Each pattern is printed with the corresponding size mentioned at the beginning of the output. The pattern consists of spaces and symbols arranged in a visually pleasing manner to form the butterfly shape.
Time Complexity:
The time complexity of this program is O(n^2), where n is the size of the pattern. The nested loops iterate up to the size-1, resulting in a quadratic time complexity.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment