C++ Program to Print matrix in snake pattern Last Updated : 12 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Given an n x n matrix in the given matrix, you have to print the elements of the matrix in the snake pattern. Examples: Input :mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}}; Output : 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Input :mat[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Output : 1 2 3 6 5 4 7 8 9Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. We traverse all rows. For every row, we check if it is even or odd. If even, we print from left to right else print from right to left. C++ // C++ program to print matrix in snake order #include <iostream> #define M 4 #define N 4 using namespace std; void print(int mat[M][N]) { // Traverse through all rows for (int i = 0; i < M; i++) { // If current row is even, print from // left to right if (i % 2 == 0) { for (int j = 0; j < N; j++) cout << mat[i][j] << " "; // If current row is odd, print from // right to left } else { for (int j = N - 1; j >= 0; j--) cout << mat[i][j] << " "; } } } // Driver code int main() { int mat[][] = { { 10, 20, 30, 40 }, { 15, 25, 35, 45 }, { 27, 29, 37, 48 }, { 32, 33, 39, 50 } }; print(mat); return 0; } Output : 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Time complexity: O(N^2) for given N*N matrix Auxiliary Space: O(1) Please refer complete article on Print matrix in snake pattern for more details! Comment More infoAdvertise with us Next Article C++ Program to Print matrix in snake pattern K kartik Follow Improve Article Tags : Matrix C++ Programs C++ DSA Practice Tags : CPPMatrix Similar Reads C++ Program to Print the Pattern 'G" In this article, we will learn how to print the pattern G using stars and white spaces. Given a number n, we will write a program to print the pattern G over n lines or rows.Examples: Input : 7 Output : *** * * * *** * * * * *** Input : 9 Output : ***** * * * * *** * * * * * * ***** In this program, 2 min read C++ Program To Print Left Half Pyramid Pattern Here, we will build a C++ program to print the left half of pyramid pattern using 2 approaches i.e. Using for loopUsing while loop1. Using for loop Input: rows = 5 Output: * ** *** **** ***** First, for loop is used to identify the number of rows and the second for loop is used to identify the numbe 3 min read C++ Program To Print Right Half Pyramid Pattern Here we will build a C++ Program To Print Right Half Pyramid Pattern with the following 2 approaches: Using for loop Using while loop Input: rows = 5 Output: * * * * * * * * * * * * * * * 1. Using for loop First for loop is used to identify the number of rows and the second for loop is used to ident 2 min read C++ Program To Print Inverted Hollow Star Pyramid Pattern Given the value of R(number of rows), write a C++ program to print the Inverted Hollow Pyramid using stars and white spaces. Examples: Input: R = 5 Output: ********* * * * * * * * Input: R = 10 Output: ******************* * * * * * * * * * * * * * * * * * Algorithm: At first, take the number of rows 2 min read C++ Program To Print Triangle Pattern Here we will see how to print triangle patterns using a C++ program. There are 4 patterns discussed here: Right Triangle.Inverted Right Triangle.Equilateral Triangle.Inverted Equilateral Triangle.Inverted Mirrored Right Triangle. Let's start discussing each of these in detail. 1. Right Triangle Belo 6 min read Like