DEV Community

Lakshyaraj Dash
Lakshyaraj Dash

Posted on

Top 21 Pattern Printing Questions

These top 21 pattern printing questions which you will need to improve your logical thinking.

Pattern 1

  • Print the following patterns
* 
* * 
* * * 
* * * * 
* * * * *
Enter fullscreen mode Exit fullscreen mode
  • Code
void pattern1(int n) {
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < i+1; j++)
        {
            cout << "* ";
        }
        cout << endl;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 2

  • Print the following patterns
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
Enter fullscreen mode Exit fullscreen mode
  • Code
void pattern2(int n) {
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j < i+1; j++)
        {
            cout << j << " ";
        }
        cout << endl;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 3

  • Print the following patterns
1 
2 2 
3 3 3
4 4 4 4
5 5 5 5 5
Enter fullscreen mode Exit fullscreen mode
  • Code
void pattern3(int n) {
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j < i+1; j++)
        {
            cout << i << " ";
        }
        cout << endl;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 4

  • Print the following patterns
* * * * *
* * * *
* * *
* *
*
Enter fullscreen mode Exit fullscreen mode
  • Code
void pattern4(int n) {
    for (int i = n; i > 0; i--)
    {
        for (int j = 0; j < i; j++)
        {
            cout << "* ";
        }
        cout << endl;   
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 5

  • Print the following patterns
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Enter fullscreen mode Exit fullscreen mode
  • Code
void pattern5(int n) {
    for (int i = n; i >= 1; i--)
    {
        for (int j = 1; j <= i; j++)
        {
            cout << j << " ";
        }
        cout << endl;   
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 6

  • Print the following patterns
    *
   ***
  *****
 *******
*********
Enter fullscreen mode Exit fullscreen mode
  • 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 7

  • Print the following patterns
*********
 *******
  *****
   ***
    *
Enter fullscreen mode Exit fullscreen mode
  • 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 8

  • Print the following patterns
    *
   ***
  *****
 *******
*********
*********
 *******
  *****
   ***
    *
Enter fullscreen mode Exit fullscreen mode
  • 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 9

  • Print the following patterns
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
Enter fullscreen mode Exit fullscreen mode
  • 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 10

  • Print the following patterns
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
Enter fullscreen mode Exit fullscreen mode
  • 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; 
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 11

  • Print the following patterns
1        1
12      21
123    321
1234  4321
1234554321
Enter fullscreen mode Exit fullscreen mode
  • 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 12

  • Print the following patterns
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Enter fullscreen mode Exit fullscreen mode
  • 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 13

  • Print the following patterns
A
A B
A B C
A B C D
A B C D E
Enter fullscreen mode Exit fullscreen mode
  • Code
void pattern13(int n) {
    for (int i = 0; i < n; i++)
    {
        for (char c = 'A'; c <= 'A' + i; c++)
        {
            cout << c << " ";
        }
        cout << endl;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 14

  • Print the following patterns
A B C D E
A B C D
A B C
A B
A
Enter fullscreen mode Exit fullscreen mode
  • 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 15

  • Print the following patterns
A
B B
C C C
D D D D
E E E E E
Enter fullscreen mode Exit fullscreen mode
  • 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++;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 16

  • Print the following patterns
    A
   ABA
  ABCBA
 ABCDCBA
ABCDEDCBA
Enter fullscreen mode Exit fullscreen mode
  • 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 17

  • Print the following patterns
E
D E
C D E
B C D E
A B C D E
Enter fullscreen mode Exit fullscreen mode
  • 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 18

  • Print the following patterns
**********
****  ****
***    ***
**      **
*        *
*        *
**      **
***    ***
****  ****
**********
Enter fullscreen mode Exit fullscreen mode
  • 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 19

  • Print the following patterns
*        *
**      **
***    ***
****  ****
**********
****  ****
***    ***
**      **
*        *
Enter fullscreen mode Exit fullscreen mode
  • 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;
    }
}
Enter fullscreen mode Exit fullscreen mode

Pattern 20

  • Print the following patterns
*****
*   *
*   *
*   *
*****
Enter fullscreen mode Exit fullscreen mode
  • 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;
    }
}

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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;
    }   
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)