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: FAQs

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)

FAQs

What is the C++ answer? ›

C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for creating large-scale applications. C++ is a superset of the C language. A related programming language, Java, is based on C++ but optimized for the distribution of program objects in a network such as the internet.

Where can I practice C++ problems? ›

Solve C++ Practice problems online with the Practice C++ path on CodeChef. Write code for over 200 C++ coding challenges and prepare for interviews. Earn certificate after completing all the problems.

How to prepare for a C++ developer interview? ›

C++ Interview Questions For Freshers
  1. What is C++? ...
  2. What are the different data types present in C++? ...
  3. Define 'std'? ...
  4. What are references in C++? ...
  5. What do you mean by Call by Value and Call by Reference? ...
  6. Define token in C++ ...
  7. What is the difference between C and C++? ...
  8. What is the difference between struct and class?
May 2, 2024

Why is C++ called ++? ›

1.3 BRIEF HISTORY OF C++

Since C++ is an attempt to add object-oriented features (plus other improvements) to C, earlier it was called as “C with Objects”. As the language developed, Stroustrup named it as C++ in 1983. The name C++ suggests “C incremented” (recall the ++ is an increment operator of C).

Why is C++ called G ++? ›

This is both the most general name for the compiler, and the name used when the emphasis is on compiling C programs (as the abbreviation formerly stood for “GNU C Compiler”). When referring to C++ compilation, it is usual to call the compiler “G++”.

Is C++ too hard for beginners? ›

C++ is somewhat difficult to learn, especially if you have never programmed before or you have never used a low-level programming language before. If you are a beginner with no programming experience, you should expect it to take at least three months to learn the basics.

Which part of C++ is difficult? ›

The Complexity of Learning C++ Language Syntax

One of the main criticisms of C++ is its syntax. C++ has a complex syntax that can be overwhelming for beginners. On the other hand, some programmers argue that the syntax is one of the strengths of the language.

What is the most important thing in C++? ›

Object-Oriented Programming

C++ is an Object-Oriented Programming Language, unlike C which is a procedural programming language. This is the most important feature of C++. It can create/destroy objects while programming. Also, It can create blueprints with which objects can be created.

What must every C++ program have? ›

function main - It is the starting point of any C++ program. Every C++ program must have a main function for the program to execute.

What must every complete C++ program have? ›

The main function. All C++ programs must have a function called main . This is the function that is executed when you start the program.

How should I practice C++ for beginners? ›

C++ Basic Exercises
  1. Write a program in C++ to find Size of fundamental data types. ...
  2. Write a program in C++ to print the sum of two numbers using variables. ...
  3. Write a program in C++ to check the upper and lower limits of integer. ...
  4. Write a program in C++ to check whether the primitive values crossing the limits or not.

Is C++ alone enough to get a job? ›

C++ is one of the most popular language, so it shouldn't be a big problem finding a job. If you have enough free time I think it is a good idea to start studying another language, like Java or Python. Try to search for your ideal job on internet, to see what employers/companies are looking for.

How many keywords are in C++? ›

1. C++ Keywords: There are 32 keyword in C and 31 new keywords are added in C++, so 63 keywords in C++. Keywords are special words whose meaning has all ready been known to the compiler. All keywords should write in small letters and cannot be used as a identifier or variable names.

What is class in C++ answer? ›

A class in C++ is a user-defined type or data structure declared with any of the keywords class , struct or union (the first two are collectively referred to as non-union classes) that has data and functions (also called member variables and member functions) as its members whose access is governed by the three access ...

What is the difference between C and C++ answer? ›

The main difference between C and C++ is that C is a procedural programming language that does not support classes and objects. On the other hand, C++ is an extension of C programming with object-oriented programming (OOP) support. Is C and C++ a compiled or interpreted language? C and C++ are both compiled languages.

What do you mean by C++ short answer? ›

C++ (or “C-plus-plus”) is a generic programming language for building software. It's an object-oriented language. In other words, it emphasizes using data fields with unique attributes (a.k.a. objects) rather than logic or functions.

What does C++ use as a comment? ›

Comments (C++)

A C++ comment is written in one of the following ways: The /* (slash, asterisk) characters, followed by any sequence of characters (including new lines), followed by the */ characters. This syntax is the same as ANSI C. The // (two slashes) characters, followed by any sequence of characters.

Top Articles
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 5667

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.