From triangles and squares to more intricate shapes like diamonds and pyramids, C++ patterns are more than just coding exercises—they’re an opportunity to unleash creativity within the framework of characters and numbers.These Pattern Solving Questions come with code and solutions in C++.
Whether you’re a coding novice looking to strengthen your skills or an experienced programmer seeking a fun challenge, these C++ pattern solving questions provide a canvas for expression and logic.
Keep practicing and Happy coding!
Table of Contents:
- Square Pattern
- Right Triangle Pattern
- Pyramid Pattern
- Inverted Pyramid Pattern
- Number Pattern
- Number Pyramid Pattern
- Square of Numbers Pattern
- Diamond Pattern
- Hollow Square with Stars Pattern
- Zigzag Star Pattern
- Hollow Square Pattern
- Butterfly Pattern
- Cross Pattern
- Spiral Pattern
- Hollow Pyramid Pattern
- Palindrome Pyramid Pattern
- Steps Pattern
- Alphabet Triangle Pattern
- Diamond Star Pattern
- Hollow Diamond Pattern
- Zigzag Pattern
- Rhombus Pattern
- Swirl Pattern
- Tree Pattern
- Hollow Right Triangle Pattern
- Number Square Pattern
- Pyramid of Stars and Numbers
- Hollow Square Pattern with Diagonals
- Zigzag Number Pattern
- Hollow Rectangle Pattern
- Upside-Down Pyramid Pattern
- Inverted Number Triangle Pattern
- Diamond of Numbers Pattern
- Hollow Right Triangle Pattern with Diagonals
- Hollow Triangle Pattern
- Diamond with Stars and Numbers Pattern
- Pyramid of Stars and Characters Pattern
- Hollow Diamond with Numbers Pattern
- Spiral Number Pattern
- Hollow Square with Diagonals Pattern
- Alphabets Triangle Pattern
- Hollow Alphabets Square Pattern
- Alphabets Right-Angled Triangle Pattern
- Hollow Alphabets Rhombus Pattern
- Alphabets Diamond Pattern
- Alphabets Hourglass Pattern
- Alphabets Diagonal Pattern
- Alphabets Zigzag Pattern
- Alphabets Cross Pattern
- Alphabets Pyramid Pattern
1. Square Pattern
Pattern : Print a square pattern of asterisks with side length n.
Example Input (n=4):
****
****
****
****
CODE:
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << '*';
}
cout << endl;
}
return 0;
}
2. Right Triangle Pattern:
Pattern: Print a right-angled triangle pattern of asterisks with height n.
Example Input (n=5):
*
**
***
****
*****
CODE:
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
cout << '*';
}
cout << endl;
}
return 0;
}
3. Pyramid Pattern:
Pattern: Print a pyramid pattern of asterisks with height n.
Example Input (n=4):
*
***
*****
*******
CODE:
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << '*';
}
cout << endl;
}
return 0;
}
4. Inverted Pyramid Pattern
Pattern: Print an inverted pyramid pattern of asterisks with height n.
Example Input (n=3):
***
**
*
CODE:
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = n; i >= 1; --i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << '*';
}
cout << endl;
}
return 0;
}
5. Number Pattern
Pattern: Print a number pattern where each row contains numbers from 1 to that row number.
Example Input (n=4):
1
12
123
1234
CODE:
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
cout << j;
}
cout << endl;
}
return 0;
}
6. Number Pyramid Pattern
Pattern: Print a number pyramid pattern with height n.
Example Input (n=4):
1
121
12321
1234321
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << k;
}
cout << endl;
}
return 0;
}
7. Square of Numbers Pattern
Pattern: Print a square pattern of numbers with side length n.
Example Input (n=3):
111
222
333
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cout << i;
}
cout << endl;
}
return 0;
}
8. Diamond Pattern
Pattern: Print a diamond pattern of asterisks with height n.
Example Input (n=5):
*
***
*****
***
*
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << '*';
}
cout << endl;
}
for (int i = n - 1; i >= 1; --i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << '*';
}
cout << endl;
}
return 0;
}
9. Hollow Square with Stars Pattern
Pattern: Print a hollow square pattern of asterisks with stars in the middle and sides.
Example Input (n=5):
*****
* *
* * *
* *
*****
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i == 1 || i == n || j == 1 || j == n || i == j || i + j == n + 1) {
cout << '*';
} else {
cout << ' ';
}
}
cout << endl;
}
return 0;
}
10. Zigzag Star Pattern
Pattern: Print a zigzag pattern of stars with height n.
Example Input (n=4):
****
****
****
****
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
if (i % 2 != 0) {
for (int j = 1; j <= n; ++j) {
cout << '*';
}
} else {
for (int j = 1; j <= n; ++j) {
cout << ' ';
}
for (int j = 1; j <= n; ++j) {
cout << '*';
}
}
cout << endl;
}
return 0;
}
11. Hollow Square Pattern
Pattern: Print a hollow square pattern of asterisks with side length n.
Example Input (n=5):
*****
* *
* *
* *
*****
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i == 1 || i == n || j == 1 || j == n) {
cout << '*';
} else {
cout << ' ';
}
}
cout << endl;
}
return 0;
}
12. Butterfly Pattern
Pattern: Print a butterfly pattern of asterisks with wingspan 2n.
Example Input (n=3):
* *
** **
*** ***
********
*** ***
** **
* *
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
cout << '*';
}
for (int j = 1; j <= 2 * (n - i); ++j) {
cout << ' ';
}
for (int j = 1; j <= i; ++j) {
cout << '*';
}
cout << endl;
}
for (int i = n; i >= 1; --i) {
for (int j = 1; j <= i; ++j) {
cout << '*';
}
for (int j = 1; j <= 2 * (n - i); ++j) {
cout << ' ';
}
for (int j = 1; j <= i; ++j) {
cout << '*';
}
cout << endl;
}
return 0;
}
13. Cross Pattern
Pattern: Print a cross pattern of asterisks with arms of length n.
Example Input (n=5):
* *
* *
*
* *
* *
* *
* *
*
* *
* *
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (j == i || j == n - i + 1) {
cout << '*';
} else {
cout << ' ';
}
}
cout << endl;
}
return 0;
}
14. Spiral Pattern
Pattern: Print a spiral pattern of numbers from 1 to n^2 in an n x n matrix.
Example Input (n=3):
1 2 3
8 9 4
7 6 5
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int matrix[n][n];
int num = 1;
int startRow = 0, endRow = n - 1, startCol = 0, endCol = n - 1;
while (startRow <= endRow && startCol <= endCol) {
for (int i = startCol; i <= endCol; ++i) {
matrix[startRow][i] = num++;
}
++startRow;
for (int i = startRow; i <= endRow; ++i) {
matrix[i][endCol] = num++;
}
--endCol;
if (startRow <= endRow) {
for (int i = endCol; i >= startCol; --i) {
matrix[endRow][i] = num++;
}
--endRow;
}
if (startCol <= endCol) {
for (int i = endRow; i >= startRow; --i) {
matrix[i][startCol] = num++;
}
++startCol;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << matrix[i][j] << ' ';
}
cout << endl;
}
return 0;
}
15. Hollow Pyramid Pattern
Pattern: Print a hollow pyramid pattern of asterisks with height n.
Example Input (n=5):
*
* *
* *
* *
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
if (k == 1 || k == 2 * i - 1 || i == n) {
cout << '*';
} else {
cout << ' ';
}
}
cout << endl;
}
return 0;
}
16. Palindrome Pyramid Pattern
Pattern: Print a palindrome pyramid pattern of numbers with height n.
Example Input (n=4):
1
121
12321
1234321
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
int num = 1;
for (int j = 1; j <= i; ++j) {
cout << num;
++num;
}
num -= 2;
for (int j = 1; j < i; ++j) {
cout << num;
--num;
}
cout << endl;
}
return 0;
}
17. Steps Pattern
Pattern: Print a steps pattern of numbers with height n.
Example Input (n=4):
1234
234
34
4
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
cout << j;
}
cout << endl;
}
return 0;
}
18. Alphabet Triangle Pattern
Pattern: Print a triangle pattern of alphabets with height n.
Example Input (n=3):
A
BC
DEF
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
char ch = 'A';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
cout << ch;
++ch;
}
cout << endl;
}
return 0;
}
19. Diamond Star Pattern
Pattern: Print a diamond star pattern with a hollow center and height n.
Example Input (n=5):
*
* *
* *
* *
*
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
if (k == 1 || k == 2 * i - 1) {
cout << '*';
} else {
cout << ' ';
}
}
cout << endl;
}
for (int i = n - 1; i >= 1; --i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
if (k == 1 || k == 2 * i - 1) {
cout << '*';
} else {
cout << ' ';
}
}
cout << endl;
}
return 0;
}
20. Hollow Diamond Pattern
Pattern: Print a hollow diamond pattern of asterisks with height n.
Example Input (n=5):
*
* *
* *
* *
* *
* *
*
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
if (k == 1 || k == 2 * i - 1) {
cout << '*';
} else {
cout << ' ';
}
}
cout << endl;
}
for (int i = n - 1; i >= 1; --i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
if (k == 1 || k == 2 * i - 1) {
cout << '*';
} else {
cout << ' ';
}
}
cout << endl;
}
return 0;
}
21. Zigzag Pattern
Pattern: Print a zigzag pattern of asterisks with height n.
Example Input (n=4):
* *
* *
* *
* *
* *
* *
* *
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i == j || i + j == n + 1) {
cout << '*';
} else {
cout << ' ';
}
}
cout << endl;
}
return 0;
}
22. Rhombus Pattern
Pattern: Print a rhombus pattern of asterisks with diagonal lengths n and m.
Example Input (n=4, m=6):
******
*******
*********
***********
*********
*******
******
#include <iostream>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n + m - 1; ++i) {
for (int j = 1; j <= n + m - 1; ++j) {
if (i + j == n + 1 || i - j == n - m + 1 || j - i == n - m + 1 || i + j == n + m * 2 - 1) {
cout << '*';
} else {
cout << ' ';
}
}
cout << endl;
}
return 0;
}
23. Swirl Pattern
Pattern: Print a swirl pattern of numbers with a spiral effect and height n.
Example Input (n=4):
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int matrix[n][n];
int num = 1;
int startRow = 0, endRow = n - 1, startCol = 0, endCol = n - 1;
while (startRow <= endRow && startCol <= endCol) {
for (int i = startRow; i <= endRow; ++i) {
matrix[i][startCol] = num++;
}
++startCol;
for (int i = startCol; i <= endCol; ++i) {
matrix[endRow][i] = num++;
}
--endRow;
if (startCol <= endCol) {
for (int i = endRow; i >= startRow; --i) {
matrix[i][endCol] = num++;
}
--endCol;
}
if (startRow <= endRow) {
for (int i = endCol; i >= startCol; --i) {
matrix[startRow][i] = num++;
}
++startRow;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << matrix[i][j] << ' ';
}
cout << endl;
}
return 0;
}
24. Tree Pattern
Pattern: Print a tree pattern of asterisks with a trunk and height n.
Example Input (n=5):
*
***
*****
*******
*********
***
***
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << '*';
}
cout << endl;
}
for (int i = 1; i <= 2; ++i) {
for (int j = 1; j <= n - 1; ++j) {
cout << ' ';
}
cout << "***" << endl;
}
return 0;
}
25. Hollow Right Triangle Pattern
Pattern: Print a hollow right-angled triangle pattern of asterisks with height n.
Example Input (n=4):
*
**
* *
* *
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
if (j == 1 || j == i || i == n) {
cout << '*';
} else {
cout << ' ';
}
}
cout << endl;
}
return 0;
}
26. Number Square Pattern
Pattern: Print a square pattern of numbers with side length n.
Example Input (n=3):
123
456
789
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int num = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cout << num;
++num;
}
cout << endl;
}
return 0;
}
27. Pyramid of Stars and Numbers
Pattern: Print a pyramid pattern alternating between stars and numbers with height n.
Example Input (n=4):
1
*2*
***3***
****4****
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int num = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
if (k % 2 == 0) {
cout << num;
++num;
} else {
cout << '*';
}
}
cout << endl;
}
return 0;
}
28. Hollow Square Pattern with Diagonals
Pattern: Print a hollow square pattern of asterisks with diagonals filled with numbers from 1 to n.
Example Input (n=5):
*123*
1*2*4
12*34
1*4*5
*345*
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i == j || i + j == n + 1 || i == 1 || i == n || j == 1 || j == n) {
cout << '*';
} else {
cout << i + j - 1;
}
}
cout << endl;
}
return 0;
}
29. Zigzag Number Pattern
Pattern: Print a zigzag pattern of numbers with height n.
Example Input (n=4):
1234
567
89
0
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int num = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
cout << num;
++num;
}
cout << endl;
}
return 0;
}
30. Hollow Rectangle Pattern
Pattern: Print a hollow rectangle pattern of asterisks with dimensions m x n.
Example Input (m=4, n=6):
******
* *
* *
******
#include <iostream>
using namespace std;
int main() {
int m, n;
cin >> m >> n;
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (i == 1 || i == m || j == 1 || j == n) {
cout << '*';
} else {
cout << ' ';
}
}
cout << endl;
}
return 0;
}
31. Upside-Down Pyramid Pattern
Pattern: Print an upside-down pyramid pattern of asterisks with height n.
Example Input (n=4):
****
***
**
*
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = n; i >= 1; --i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << '*';
}
cout << endl;
}
return 0;
}
32. Inverted Number Triangle Pattern
Pattern: Print an inverted triangle pattern of numbers with height n.
Example Input (n=4):
4321
432
43
4
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = n; i >= 1; --i) {
for (int j = n; j >= i; --j) {
cout << j;
}
cout << endl;
}
return 0;
}
33. Diamond of Numbers Pattern
Pattern: Print a diamond pattern of numbers with height n.
Example Input (n=3):
1
123
12345
321
1
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << k;
}
cout << endl;
}
for (int i = n - 1; i >= 1; --i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << k;
}
cout << endl;
}
return 0;
}
34. Hollow Right Triangle Pattern with Diagonals
Pattern: Print a hollow right-angled triangle pattern of asterisks with diagonals filled with numbers from 1 to n.
Example Input (n=4):
1
*2
**3
* 4
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
if (j == 1 || j == i || i == n) {
cout << j;
} else {
cout << ' ';
}
}
cout << endl;
}
return 0;
}
35. Hollow Triangle Pattern
Pattern: Print a hollow triangle pattern of asterisks with height n.
Example Input (n=5):
*****
* *
* *
**
*
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
if (i == n || j == 1 || j == i) {
cout << '*';
} else {
cout << ' ';
}
}
cout << endl;
}
return 0;
}
36. Diamond with Stars and Numbers Pattern
Pattern: Print a diamond pattern with both stars and numbers with height n.
Example Input (n=4):
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int num = 1;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
if (k % 2 == 0) {
cout << num;
++num;
} else {
cout << '*';
}
}
cout << endl;
}
num = n * 2 - 3;
for (int i = n - 1; i >= 1; --i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
if (k % 2 == 0) {
cout << num;
++num;
} else {
cout << '*';
}
}
cout << endl;
}
return 0;
}
37. Pyramid of Stars and Characters Pattern
Pattern: Print a pyramid pattern alternating between stars and characters with height n.
Example Input (n=4):
A
*B*
***C***
****D****
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
char ch = 'A';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
if (k % 2 == 0) {
cout << ch;
++ch;
} else {
cout << '*';
}
}
cout << endl;
}
return 0;
}
38. Hollow Diamond with Numbers Pattern
Pattern: Print a hollow diamond pattern with numbers filling the diagonals and height n.
Example Input (n=5):
1
2*2
3*3*3
4*4*4*4
3*3*3
2*2
1
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
if (k == 1 || k == 2 * i - 1) {
cout << i;
} else {
cout << '*';
}
}
cout << endl;
}
for (int i = n - 1; i >= 1; --i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
if (k == 1 || k == 2 * i - 1) {
cout << i;
} else {
cout << '*';
}
}
cout << endl;
}
return 0;
}
39. Spiral Number Pattern
Pattern: Print a spiral pattern of numbers with a square matrix of side length n.
Example Input (n=4):
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
int matrix[n][n];
int num = 1;
int startRow = 0, endRow = n - 1, startCol = 0, endCol = n - 1;
while (startRow <= endRow && startCol <= endCol) {
for (int i = startRow; i <= endRow; ++i) {
matrix[i][startCol] = num++;
}
++startCol;
for (int i = startCol; i <= endCol; ++i) {
matrix[endRow][i] = num++;
}
--endRow;
if (startCol <= endCol) {
for (int i = endRow; i >= startRow; --i) {
matrix[i][endCol] = num++;
}
--endCol;
}
if (startRow <= endRow) {
for (int i = endCol; i >= startCol; --i) {
matrix[startRow][i] = num++;
}
++startRow;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cout << matrix[i][j] << ' ';
}
cout << endl;
}
return 0;
}
40. Hollow Square with Diagonals Pattern
Pattern: Print a hollow square pattern of asterisks with diagonals filled with numbers from 1 to n.
Example Input (n=5):
*123*
1*2*4
12*34
1*4*5
*345*
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i == j || i + j == n + 1 || i == 1 || i == n || j == 1 || j == n) {
cout << '*';
} else {
cout << j + 1;
}
}
cout << endl;
}
return 0;
}
41. Alphabets Triangle Pattern:
Pattern: Print a triangle pattern of alphabets with height n.
Example Input (n=4):
A
BC
DEF
GHIJ
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
char ch = 'A';
for (int i = 1, count = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
cout << ch;
++ch;
}
cout << endl;
}
return 0;
}
42. Hollow Alphabets Square Pattern:
Pattern: Print a hollow square pattern of alphabets with side length n.
Example Input (n=4):
ABCD
B E
C F
DEFG
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
char ch = 'A';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i == 1 || i == n || j == 1 || j == n) {
cout << ch;
} else {
cout << ' ';
}
++ch;
}
cout << endl;
}
return 0;
}
43. Alphabets Right-Angled Triangle Pattern:
Pattern: Print a right-angled triangle pattern of alphabets with height n.
Example Input (n=4):
A
AB
ABC
ABCD
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
char ch = 'A';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= i; ++j) {
cout << ch;
++ch;
}
cout << endl;
}
return 0;
}
44. Hollow Alphabets Rhombus Pattern:
Pattern: Print a hollow rhombus pattern of alphabets with side lengths n and m.
Example Input (n=4, m=6):
ABCD
B E
C F
DEFG
#include <iostream>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
char ch = 'A';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= m; ++k) {
if (i == 1 || i == n || k == 1 || k == m) {
cout << ch;
} else {
cout << ' ';
}
++ch;
}
cout << endl;
}
return 0;
}
45. Alphabets Diamond Pattern:
Pattern: Print a diamond pattern of alphabets with height n.
Example Input (n=4):
A
BCB
CDCD
EDEDE
CDCD
BCB
A
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
char ch = 'A';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << ch;
if (k < i) {
++ch;
} else {
--ch;
}
}
cout << endl;
ch = 'A';
}
for (int i = n - 1; i >= 1; --i) {
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << ch;
if (k < i) {
++ch;
} else {
--ch;
}
}
cout << endl;
ch = 'A';
}
return 0;
}
46. Alphabets Hourglass Pattern:
Pattern: Print an hourglass pattern of alphabets with dimensions n x m.
Example Input (n=3, m=5):
ABCDE
FG
HIJKL
MN
OPQR
#include <iostream>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
char ch = 'A';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cout << ch;
++ch;
}
cout << endl;
if (i % 2 == 0) {
ch = 'A';
ch += i * m;
}
}
return 0;
}
47. Alphabets Diagonal Pattern:
Pattern: Print a diagonal pattern of alphabets with dimensions n x n.
Example Input (n=4):
A
B
C
D
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
char ch = 'A';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (j == i) {
cout << ch;
} else {
cout << ' ';
}
}
cout << endl;
++ch;
}
return 0;
}
48. Alphabets Zigzag Pattern:
Pattern: Print a zigzag pattern of alphabets with dimensions n x m.
Example Input (n=4, m=5):
ABCDE
F J
K O
PQRST
#include <iostream>
using namespace std;
int main() {
int n, m;
cin >> n >> m;
char ch = 'A';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= m; ++j) {
cout << ch;
++ch;
}
cout << endl;
if (i % 2 == 0) {
ch += m;
}
}
return 0;
}
49. Alphabets Cross Pattern:
Pattern: Print a cross pattern of alphabets with dimensions n x n.
Example Input (n=5):
A A
B B
C
D D
E E
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
char ch = 'A';
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i == j || i + j == n + 1) {
cout << ch;
} else {
cout << ' ';
}
}
cout << endl;
++ch;
}
return 0;
}
50. Alphabets Pyramid Pattern:
Pattern: Print a pyramid pattern of alphabets with dimensions n x n.
Example Input (n=4):
A
ABA
ABCBA
ABCDCBA
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
char ch;
for (int i = 1; i <= n; ++i) {
ch = 'A';
for (int j = 1; j <= n - i; ++j) {
cout << ' ';
}
for (int k = 1; k <= 2 * i - 1; ++k) {
cout << ch;
if (k < i) {
++ch;
} else {
--ch;
}
}
cout << endl;
}
return 0;
}
Keep practicing and Happy coding!