50+ Pattern Solving Questions in C++ with Answers - Coding Aunty (2024)

Table of Contents
Table of Contents: 1. Square Pattern 2. Right Triangle Pattern: 3. Pyramid Pattern: 4. Inverted Pyramid Pattern 5. Number Pattern 6. Number Pyramid Pattern 7. Square of Numbers Pattern 8. Diamond Pattern 9. Hollow Square with Stars Pattern 10. Zigzag Star Pattern 11. Hollow Square Pattern 12. Butterfly Pattern 13. Cross Pattern 14. Spiral Pattern 15. Hollow Pyramid Pattern 16. Palindrome Pyramid Pattern 17. Steps Pattern 18. Alphabet Triangle Pattern 19. Diamond Star Pattern 20. Hollow Diamond Pattern 21. Zigzag Pattern 22. Rhombus Pattern 23. Swirl Pattern 24. Tree Pattern 25. Hollow Right Triangle Pattern 26. Number Square Pattern 27. Pyramid of Stars and Numbers 28. Hollow Square Pattern with Diagonals 29. Zigzag Number Pattern 30. Hollow Rectangle Pattern 31. Upside-Down Pyramid Pattern 32. Inverted Number Triangle Pattern 33. Diamond of Numbers Pattern 34. Hollow Right Triangle Pattern with Diagonals 35. Hollow Triangle Pattern 36. Diamond with Stars and Numbers Pattern 37. Pyramid of Stars and Characters Pattern 38. Hollow Diamond with Numbers Pattern 39. Spiral Number Pattern 40. Hollow Square with Diagonals Pattern 41. Alphabets Triangle Pattern: 42. Hollow Alphabets Square Pattern: 43. Alphabets Right-Angled Triangle Pattern: 44. Hollow Alphabets Rhombus Pattern: 45. Alphabets Diamond Pattern: 46. Alphabets Hourglass Pattern: 47. Alphabets Diagonal Pattern: 48. Alphabets Zigzag Pattern: 49. Alphabets Cross Pattern: 50. Alphabets Pyramid Pattern: References

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:

  1. Square Pattern
  2. Right Triangle Pattern
  3. Pyramid Pattern
  4. Inverted Pyramid Pattern
  5. Number Pattern
  6. Number Pyramid Pattern
  7. Square of Numbers Pattern
  8. Diamond Pattern
  9. Hollow Square with Stars Pattern
  10. Zigzag Star Pattern
  11. Hollow Square Pattern
  12. Butterfly Pattern
  13. Cross Pattern
  14. Spiral Pattern
  15. Hollow Pyramid Pattern
  16. Palindrome Pyramid Pattern
  17. Steps Pattern
  18. Alphabet Triangle Pattern
  19. Diamond Star Pattern
  20. Hollow Diamond Pattern
  21. Zigzag Pattern
  22. Rhombus Pattern
  23. Swirl Pattern
  24. Tree Pattern
  25. Hollow Right Triangle Pattern
  26. Number Square Pattern
  27. Pyramid of Stars and Numbers
  28. Hollow Square Pattern with Diagonals
  29. Zigzag Number Pattern
  30. Hollow Rectangle Pattern
  31. Upside-Down Pyramid Pattern
  32. Inverted Number Triangle Pattern
  33. Diamond of Numbers Pattern
  34. Hollow Right Triangle Pattern with Diagonals
  35. Hollow Triangle Pattern
  36. Diamond with Stars and Numbers Pattern
  37. Pyramid of Stars and Characters Pattern
  38. Hollow Diamond with Numbers Pattern
  39. Spiral Number Pattern
  40. Hollow Square with Diagonals Pattern
  41. Alphabets Triangle Pattern
  42. Hollow Alphabets Square Pattern
  43. Alphabets Right-Angled Triangle Pattern
  44. Hollow Alphabets Rhombus Pattern
  45. Alphabets Diamond Pattern
  46. Alphabets Hourglass Pattern
  47. Alphabets Diagonal Pattern
  48. Alphabets Zigzag Pattern
  49. Alphabets Cross Pattern
  50. 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):

1121231234

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 123211234321
#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):

111222333
#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 38 9 47 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 123211234321
#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):

ABCDEF
#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 412 13 14 511 16 15 610 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):

123456789
#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*412*341*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):

4321432434
#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*34*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 412 13 14 511 16 15 610 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*412*341*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):

ABCDEFGHIJ
#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):

ABCDB EC FDEFG
#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):

AABABCABCD
#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 EC FDEFG
#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 CDCDEDEDE 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 FGHIJKL MNOPQR
#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):

ABCDEF JK OPQRST
#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 DE 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 ABCBAABCDCBA
#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!

50+ Pattern Solving Questions in C++ with Answers - Coding Aunty (2024)

References

Top Articles
Latest Posts
Article information

Author: Patricia Veum II

Last Updated:

Views: 6469

Rating: 4.3 / 5 (44 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.