These top 21 pattern printing questions which you will need to improve your logical thinking.
Pattern 1
- Print the following patterns
*
* *
* * *
* * * *
* * * * *
- Code
void pattern1(int n) {
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i+1; j++)
{
cout << "* ";
}
cout << endl;
}
}
Pattern 2
- Print the following patterns
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
- Code
void pattern2(int n) {
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < i+1; j++)
{
cout << j << " ";
}
cout << endl;
}
}
Pattern 3
- Print the following patterns
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
- Code
void pattern3(int n) {
for (int i = 1; i <= n; i++)
{
for (int j = 1; j < i+1; j++)
{
cout << i << " ";
}
cout << endl;
}
}
Pattern 4
- Print the following patterns
* * * * *
* * * *
* * *
* *
*
- Code
void pattern4(int n) {
for (int i = n; i > 0; i--)
{
for (int j = 0; j < i; j++)
{
cout << "* ";
}
cout << endl;
}
}
Pattern 5
- Print the following patterns
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
- Code
void pattern5(int n) {
for (int i = n; i >= 1; i--)
{
for (int j = 1; j <= i; j++)
{
cout << j << " ";
}
cout << endl;
}
}
Pattern 6
- Print the following patterns
*
***
*****
*******
*********
- Code
void pattern6(int n) {
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n-i-1; j++)
{
cout << " ";
}
for (int j = 0; j < 2*i+1; j++)
{
cout << "*";
}
for (int j = 0; j < n-i-1; j++)
{
cout << " ";
}
cout << endl;
}
}
Pattern 7
- Print the following patterns
*********
*******
*****
***
*
- Code
void pattern7(int n) {
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
cout << " ";
}
for (int j = 0; j < 2*n - (2*i+1); j++)
{
cout << "*";
}
for (int j = 0; j < i; j++)
{
cout << " ";
}
cout << endl;
}
}
Pattern 8
- Print the following patterns
*
***
*****
*******
*********
*********
*******
*****
***
*
- Code
void pattern8(int n) {
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n-i-1; j++)
{
cout << " ";
}
for (int j = 0; j < 2*i+1; j++)
{
cout << "*";
}
for (int j = 0; j < n-i-1; j++)
{
cout << " ";
}
cout << endl;
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < i; j++)
{
cout << " ";
}
for (int j = 0; j < 2*n - (2*i+1); j++)
{
cout << "*";
}
for (int j = 0; j < i; j++)
{
cout << " ";
}
cout << endl;
}
}
Pattern 9
- Print the following patterns
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
- Code
void pattern9(int n) {
for (int i = 1; i <= 2*n-1; i++)
{
int stars = i;
if (i > n) stars = 2*n - i;
for (int j = 1; j <= stars; j++)
{
cout << "* ";
}
cout << endl;
}
}
Pattern 10
- Print the following patterns
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
- Code
void pattern10(int n) {
int start = 1;
for (int i = 0; i < n; i++)
{
if (i%2 != 0) start = 0;
else start = 1;
for (int j = 0; j <= i; j++)
{
cout << start << " ";
start = 1 - start;
}
cout << endl;
}
}
Pattern 11
- Print the following patterns
1 1
12 21
123 321
1234 4321
1234554321
- Code
void pattern11(int n) {
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
cout << j;
}
for (int j = 1; j <= 2*(n-i); j++)
{
cout << " ";
}
for (int j=i; j>=1; j--) {
cout << j;
}
cout << endl;
}
}
Pattern 12
- Print the following patterns
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
- Code
void pattern12(int n) {
int num = 1;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
cout << num << " ";
num += 1;
}
cout << endl;
}
}
Pattern 13
- Print the following patterns
A
A B
A B C
A B C D
A B C D E
- Code
void pattern13(int n) {
for (int i = 0; i < n; i++)
{
for (char c = 'A'; c <= 'A' + i; c++)
{
cout << c << " ";
}
cout << endl;
}
}
Pattern 14
- Print the following patterns
A B C D E
A B C D
A B C
A B
A
- Code
void pattern14(int n) {
for (int i = n-1; i >= 0; i--)
{
for (char c = 'A'; c <= 'A' + i; c++) {
cout << c << " ";
}
cout << endl;
}
}
Pattern 15
- Print the following patterns
A
B B
C C C
D D D D
E E E E E
- Code
void pattern15(int n) {
char c = 'A';
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
cout << c << " ";
}
cout << endl;
c++;
}
}
Pattern 16
- Print the following patterns
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
- Code
void pattern16(int n) {
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n-i-1; j++)
{
cout << " ";
}
char c = 'A';
int breakpt = i;
for (int j = 0; j < 2*i+1; j++)
{
cout << c;
if (j < breakpt) c++;
else c--;
}
for (int j = 0; j < n-i-1; j++)
{
cout << " ";
}
cout << endl;
}
}
Pattern 17
- Print the following patterns
E
D E
C D E
B C D E
A B C D E
- Code
void pattern17(int n) {
for (int i = 0; i < n; i++)
{
char c = 'E' - i;
for (c; c <= 'E'; c++) {
cout << c << " ";
}
cout << endl;
}
}
Pattern 18
- Print the following patterns
**********
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
**********
- Code
void pattern18(int n) {
int inis = 0;
for (int i = 0; i < n; i++)
{
for (int j = 1; j <= n-i; j++) {
cout << "*";
}
for (int j=0; j < inis; j++) {
cout << " ";
}
for (int j=1; j <= n-i; j++) {
cout << "*";
}
cout << endl;
inis += 2;
}
inis = 2*n-2;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++) {
cout << "*";
}
for (int j=0; j < inis; j++) {
cout << " ";
}
for (int j=1; j <= i; j++) {
cout << "*";
}
cout << endl;
inis -= 2;
}
}
Pattern 19
- Print the following patterns
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
- Code
void pattern19(int n) {
int inis = 2 * n - 2;
for (int i = 1; i <= 2 * n - 1; i++)
{
int stars = i;
if (i > n) stars = 2 * n - i;
for (int j = 1; j <= stars; j++) {
cout << "*";
}
for (int j = 1; j <= inis; j++) {
cout << " ";
}
for (int j = 1; j <= stars; j++) {
cout << "*";
}
cout << endl;
if (i < n) inis -= 2;
else inis += 2;
}
}
Pattern 20
- Print the following patterns
*****
* *
* *
* *
*****
- Code
void pattern20(int n) {
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == 0 || j == 0 || i == n-1 || j == n-1) {
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
}
Pattern 21
- Print the following patterns
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5
- Code
void pattern21(int n) {
for (int i = 0; i < 2*n-1; i++)
{
for (int j = 0; j < 2*n-1; j++)
{
int top = i;
int left = j;
int right = (2*n - 2) - j;
int bottom = (2*n - 2) - i;
cout << (n - min(min(top, bottom), min(right, left))) << " ";
}
cout << endl;
}
}
Top comments (0)