PATTERN PROGRAMMING
EEE 1109 [Fall,24]
By Mohammad Minhaz Akram
Pattern: 1
#include<iostream>
using namespace std;
int main()
{
int N;
cout << "Enter he number of rows: ";
cin >> N;
for(int i=1;i<=N;i++)
{
cout << "Row "<<i<<" : *"<<endl;
}
}
Enter he number of rows: 8
Row 1 : *
Row 2 : *
Row 3 : *
Row 4 : *
Row 5 : *
Row 6 : *
Row 7 : *
Row 8 : *
Pattern: 2
#include<iostream>
using namespace std;
int main()
{
int N;
cout << "Enter he number of rows: ";
cin >> N;
int M;
cout << "Enter he number of Columns: ";
cin >> M;
for(int i=1;i<=N;i++)
{
for(int i=1;i<=N;i++)
{
cout <<" *";
}
cout <<endl;
}
}
Enter he number of rows: 8
Enter he number of Columns: 5
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
Pattern: 3
#include<iostream>
using namespace std;
int main()
{
int N;
cout << "Enter he number of rows: ";
cin >> N;
for(int r=1;r<=N;r++)
{
for(int c=1;c<=r;c++)
{
cout <<" *";
}
cout <<endl;
}
}
Enter he number of rows: 7
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
Pattern: 4
#include<iostream>
using namespace std;
int main()
{
int N;
cout << "Enter he number of rows: ";
cin >> N;
/// UPPER
for(int r=1;r<=N;r++)
{
for(int c=1;c<=r;c++)
{
cout <<" *";
}
cout <<endl;
}
/// LOWER
for(int r=N-1;r>0;r--)
{
for(int c=1;c<=r;c++)
{
cout <<" *";
}
cout <<endl;
}
}
Enter he number of rows: 8
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Pattern: 5
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int N;
cout << "Enter he number of rows: ";
cin >> N;
/// UPPER
for(int r=1;r<=N;r++)
{
for(int c=1;c<=r;c++)
{
cout <<setw(3)<< r;
}
cout <<endl;
}
/// LOWER
for(int r=N-1;r>0;r--)
{
for(int c=1;c<=r;c++)
{
cout <<setw(3)<<r;
}
cout <<endl;
}
}
Enter he number of rows: 6
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
5 5 5 5 5
4 4 4 4
3 3 3
2 2
1
Pattern: 6
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int N;
char sym1,sym2;
cout <<"Enter symbol for Odd Rows: ";
cin >> sym1;
cout <<"Enter symbol for Even Rows: ";
cin >> sym2;
cout << "Enter the number of rows: ";
cin >> N;
/// UPPER
for(int r=1;r<=N;r++)
{
for(int c=1;c<=r;c++)
{
cout <<setw(3)<< (r%2==0? sym2:sym1);
}
cout <<endl;
}
/// LOWER
for(int r=N-1;r>0;r--)
{
for(int c=1;c<=r;c++)
{
cout <<setw(3)<<(r%2==0? sym2:sym1);
}
cout <<endl;
}
}
Enter symbol for Odd Rows: ^
Enter symbol for Even Rows: @
Enter the number of rows: 7
^
@ @
^ ^ ^
@ @ @ @
^ ^ ^ ^ ^
@ @ @ @ @ @
^ ^ ^ ^ ^ ^ ^
@ @ @ @ @ @
^ ^ ^ ^ ^
@ @ @ @
^ ^ ^
@ @
^
Pattern: 7
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int N;
cout << "Enter the number of rows: ";
cin >> N;
/// UPPER
for(int r=1;r<=N;r++)
{
for(int c=1;c<=r;c++)
{
cout <<setw(3)<< char(64+r);
}
cout <<endl;
}
/// LOWER
for(int r=N-1;r>0;r--)
{
for(int c=1;c<=r;c++)
{
cout <<setw(3)<<char(64+r);
}
cout <<endl;
}
}
Enter the number of rows: 9
A
B B
C C C
D D D D
E E E E E
F F F F F F
G G G G G G G
H H H H H H H H
I I I I I I I I I
H H H H H H H H
G G G G G G G
F F F F F F
E E E E E
D D D D
C C C
B B
A
Pattern: 8
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int N;
cout << "Enter the number of rows: ";
cin >> N;
/// UPPER
for(int r=1;r<=N;r++)
{
for(int c=1;c<=r;c++)
{
cout <<setw(3)<< char('a'+r-1);
}
cout <<endl;
}
/// LOWER
for(int r=N-1;r>0;r--)
{
for(int c=1;c<=r;c++)
{
cout <<setw(3)<<char('a'+r-1);
}
cout <<endl;
}
}
Enter the number of rows: 9
a
b b
c c c
d d d d
e e e e e
f f f f f f
g g g g g g g
h h h h h h h h
i i i i i i i i i
h h h h h h h h
g g g g g g g
f f f f f f
e e e e e
d d d d
c c c
b b
a
Pattern: 9
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int N;
cout << "Enter the number of rows: ";
cin >> N;
/// UPPER
for(int r=1;r<=N;r++)
{
for(int g=1;g<=N-r;g++)/// Gap Generation, total Col=N, symbol=r, gap= N-r
{
cout <<setw(3)<< ' ';
}
for(int c=1;c<=r;c++)
{
cout <<setw(3)<< 'x';
}
cout <<endl;
}
}
Enter the number of rows: 8
x
x x
x x x
x x x x
x x x x x
x x x x x x
x x x x x x x
x x x x x x x x
Pattern: 10
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int N;
cout << "Enter the number of rows: ";
cin >> N;
for(int r=1;r<=N;r++)
{
for(int g=1;g<=N-r;g++)
{
cout <<setw(3)<< ' ';
}
for(int c=1;c<=2*r-1;c++)
{
cout <<setw(3)<< 'x';
}
cout <<endl;
}
}
Enter the number of rows: 7
x
x x x
x x x x x
x x x x x x x
x x x x x x x x x
x x x x x x x x x x x
x x x x x x x x x x x x x
Pattern: 11
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int N;
cout << "Enter the number of rows: ";
cin >> N;
N= (N%2==0? N/2:(N+2)/2);
/// UPPER
for(int r=1;r<=N;r++)
{
for(int g=1;g<=N-r;g++)
{
cout <<setw(3)<< ' ';
}
for(int c=1;c<=2*r-1;c++)
{
cout <<setw(3)<< 'x';
}
cout <<endl;
}
/// LOWER
for(int r=N-1;r>0;r--)
{
for(int g=1;g<=N-r;g++)
{
cout <<setw(3)<< ' ';
}
for(int c=1;c<=2*r-1;c++)
{
cout <<setw(3)<< 'x';
}
cout <<endl;
}
}
Enter the number of rows: 8
x
x x x
x x x x x
x x x x x x x
x x x x x
x x x
x
Pattern: 12 [SAND CLOCK]
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int N;
cout << "Enter the number of rows: ";
cin >> N;
N= (N%2==0? N/2:(N+2)/2);
/// UPPER
for(int r=N;r>0;r--)
{
for(int g=1;g<=N-r;g++)
{
cout <<setw(3)<< ' ';
}
for(int c=1;c<=2*r-1;c++)
{
cout <<setw(3)<< 'x';
}
cout <<endl;
}
/// LOWER
for(int r=2;r<=N;r++)
{
for(int g=1;g<=N-r;g++)
{
cout <<setw(3)<< ' ';
}
for(int c=1;c<=2*r-1;c++)
{
cout <<setw(3)<< 'x';
}
cout <<endl;
}
}
Enter the number of rows: 9
x x x x x x x x x
x x x x x x x
x x x x x
x x x
x
x x x
x x x x x
x x x x x x x
x x x x x x x x x
Pattern: 13 [RHOMBUS]
Total Row: 2N-1
Upper Segment: N rows
Lower Segment: N-1 rows
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int N;
cout << " Total number of rows = 2N-1 ";
cout << "Enter the number N: ";
cin >> N;
/// UPPER
for(int r=1;r<=N;r++)
{
for(int g=1;g<=N-r;g++)
{
cout <<setw(3)<< ' ';
}
for(int c=1;c<=r;c++)
{
cout <<setw(3)<< 'x';
}
for(int c=1;c<=r-1;c++)
{
cout <<setw(3)<< 'x';
}
cout <<endl;
}
/// LOWER
for(int r=N-1;r>0;r--)
{
for(int g=1;g<=N-r;g++)
{
cout <<setw(3)<< ' ';
}
for(int c=1;c<=r;c++)
{
cout <<setw(3)<< 'x';
}
for(int c=1;c<=r-1;c++)
{
cout <<setw(3)<< 'x';
}
cout <<endl;
}
}
Total number of rows = 2N-1 Enter the number N: 5
x
x x x
x x x x x
x x x x x x x
x x x x x x x x x
x x x x x x x
x x x x x
x x x
x
Pattern: 14 [BUTTERFLY]
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int N;
cout << " Total number of rows = 2N-1 ";
cout << "Enter the number N: ";
cin >> N;
/// UPPER
for(int r=1;r<=N;r++)
{
for(int c=1;c<=r;c++)
{
cout <<setw(3)<< 'x';
}
for(int g=1;g<=2*N-2*r;g++)
{
cout <<setw(3)<< ' ';
}
for(int c=1;c<=r;c++)
{
cout <<setw(3)<< 'x';
}
cout <<endl;
}
/// LOWER
for(int r=N-1;r>0;r--)
{
for(int c=1;c<=r;c++)
{
cout <<setw(3)<< 'x';
}
for(int g=1;g<=2*N-2*r;g++)
{
cout <<setw(3)<< ' ';
}
for(int c=1;c<=r;c++)
{
cout <<setw(3)<< 'x';
}
cout <<endl;
}
}
Total number of rows = 2N-1 Enter the number N: 5
x x
x x x x
x x x x x x
x x x x x x x x
x x x x x x x x x x
x x x x x x x x
x x x x x x
x x x x
x x