Â
Hello Readers,
In this post we are going to see, how to print "BUTTERFLY PATTERN" OF SPECIAL CAHARCTERS in C++, JAVA, PYTHON which is as follows:-
Logic to print the "BUTTERFLY" Pattern of numbers:-
1. Assign five variables as n,c,i,j,k.
2. Use variable 'n' to take input of number of rows from the user.
3. Use variable 'c' to take the character from the user to print.
4. Now Use For Loop and use variable 'i' as iterator, its value will start from 1 till the input value i.e.(n),use post incrementation for 'i'.
5. Using Nested For Loop(loop inside another loop) take another iterator as 'j', start its value from 1 and it will last till the value of 'i', use post incrementation for 'j'.
6. Inside this loop print the character that has been taken as input i.e. (value of 'c').
7. Outside this loop declare a variable 'space' as (2*n-2) * i.
8. Take another loop with iterator 'j', start its value from 1 till the value of 'space', use post incrementation for 'j'.
9. Inside this loop print the gap between the columns, i.e. (" ").
10. outside this loop, take another loop with iterator 'k' , assign the value of 'k' as '1', it value will last till the value of 'i' , use post incrementation for 'k'.
11. inside this loop, print the value of 'c'.
12. outside this loop, print the code for the next line i.e ("\n" ).
13. close the first loop with iterator 'i'.
14. Now create a loop with iterator 'i', its value will start from (n-1), its value will be checked in the loop whether it is greater than or equal to 1 or not (i>=1) use post decrementation for 'i'. (From this the right wing of the Butterfly Pattern begins)
15. Using Nested for loop with iterator 'j' , its value will last till the value of 'i', use post incrementation for 'j'.
16. Inside this loop print the value of 'c'.
17. Now outside this loop declare a variable 'space' as 2 *(n-2) * i.
18. Now take another loop with iterator 'j' its value will be till the value of space, use post incrementation for 'j'.
19. Inside this loop print the space between the columns i.e. (" ").
20. Take a loop with iterator 'k' initialize it as 1, its value will last till the value of 'i' (k<=i), use post incrementation for 'k'.
21. Inside this loop print the value of 'c'.
22. outside this loop, print the code for the next line i.e ("\n").
23. After this close all the loops.
24. In this way you can print the "BUTTERFLY PATTERN".
BUTTERFLY PATTERN:
@Â Â Â Â Â Â Â @
@@Â Â Â Â @@
@@@@@@
@@Â Â Â Â @@
@Â Â Â Â Â Â Â @
@@Â Â Â Â @@
@@@@@@
@@Â Â Â Â @@
@Â Â Â Â Â Â Â @
C++
//**HELPFORCODERS**
//This is a Pattern project which prints
//# #
//## ##
//### ###
//########
//### ###
//## ##
//# #
#include <iostream>
using namespace std;
int main()
{
int n;
cout << "Enter the number of rows\n";
cin >> n;
cout << "Enter any Character to print the pattern\n";
char c;
cin >> c;
cout << endl;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
cout << c;
}
int space = 2 * n - 2 * i;
for (int j = 1; j <= space; j++)
{
cout << " ";
}
for (int k = 1; k <= i; k++)
{
cout << c;
}
cout << endl;
}
for (int i = n-1; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
cout << c;
}
int space = 2 * n - 2 * i;
for (int j = 1; j <= space; j++)
{
cout << " ";
}
for (int k = 1; k <= i; k++)
{
cout << c;
}
cout << endl;
}
return 0;
}
  Â
Output For C++
Java
//**HELPFORCODERS**
//This is a Pattern project which prints
//# #
//## ##
//### ###
//########
//### ###
//## ##
//# #
package com.company;
import java.util.*;
public class ButterflyPattern
{
public static void main(String[] args)
{
int rows;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Number of Rows ");
rows = scanner.nextInt();
System.out.println("Enter any character to print the pattern");
char c;
c = scanner.next().charAt(0);
System.out.print("\n");
for(int i=1; i<=rows; i++)
{
for (int j=1;j<=i;j++)
{
System.out.print(c);
}
int space = 2*rows-2*i;
for (int j=1;j<=space;j++)
{
System.out.print(" ");
}
for (int k=1;k<=i;k++)
{
System.out.print(c);
}
System.out.print("\n");
}
for (int i=rows-1;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
System.out.print(c);
}
int space = 2*rows-2*i;
for (int j=1;j<=space;j++)
{
System.out.print(" ");
}
for (int k=1;k<=i;k++)
{
System.out.print(c);
}
System.out.print("\n");
}
}
}
Output for JAVA
Python
#**HELPFORCODERS**
#This is a Pattern project which prints
#@ @
#@@ @@
#@@@ @@@
#@@@@@@@@
#@@@ @@@
#@@ @@
#@ @
rows = int(input("Enter number of rows\n"))
c = str(input("Enter any character to print the pattern\n"))
print("\n")
# Upper part
for i in range(1, rows+1):
for j in range(1, 2*rows+1):
if j > i and j < 2*rows+1-i:
print(" ", end="")
else:
print(c, end="")
print()
# Lower part
for i in range(rows-1, 0, -1):
for j in range(2*rows, 0, -1):
if j > i and j < 2*rows+1-i:
print(" ", end="")
else:
print(c, end="")
print()
Output for PYTHON
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP