Â
In this, we are going to see how to print the above pattern of Butterfly, using nested for loops in C++.
The code given below can be used for TURBO C++ Compiler:-
//pattern 20
#include <iostream.h>
#include<conio.h>
void main()
{
clrscr();
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;
}
getch();
}
The code given below can be used for g++/gcc Compiler:-
//pattern 20
#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;
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP