
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Print matrix in snake pattern in C Programming.
Given an array of nxn size, the program must print the elements of an array in a snake pattern without doing any changes to their original locations
Example
Input: arr[]= 100 99 98 97 93 94 95 96 92 91 90 89 85 86 87 88 Output: 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85
The program will traverse each row of a matrix and check for even or odd rows.
If the row is even it will print the elements of that row from left to right
If the row is odd it will print the elements of that row from right to left
Algorithm
START Step 1 -> create header files for declaring rows and column let’s say of size 4x4 Step 2 -> declare initial variables i and j and array[][] with elements Step 3 -> Loop For i=0 and i<M and i++ IF i%2==0 Loop For j=0 and j<N and j++ Print arr[i][j] End End Else Loop For j=N-1 and j>=0 and j— Print arr[i][j] End End STOP
Example
#include<stdio.h> #define M 4 #define N 4 int main() { int i,j; int arr[M][N] = { { 100, 99, 98, 97 }, { 93, 94, 95, 96 }, { 92, 91, 90, 89 }, { 85, 86, 87, 88 } }; for (i = 0; i < M; i++) { //for rows if (i % 2 == 0) { for (j = 0; j < N; j++) // for column printf("%d ",arr[i][j]); } else{ for (j = N - 1; j >= 0; j--) printf("%d ",arr[i][j]); } } return 0; }
Output
if we run the above program then it will generate the following output
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85
- Related Articles
- Print matrix in snake pattern from the last column in C Programming.
- Python program to print matrix in a snake pattern
- Print matrix in diagonal pattern
- Print numbers in matrix diagonal pattern in C Program.
- Print matrix in zag-zag fashion in C Programming.
- Print concentric rectangular pattern in a 2d matrix in C++
- Print lower triangular matrix pattern from given array in C Program.
- Program to print a matrix in Diagonal Pattern.
- Swift Program to Print Diagonal Matrix Pattern
- Print a 2 D Array or Matrix in Java Programming
- Program to print Interesting pattern in C++
- Program to print Kite Pattern in C++
- Program to print number pattern in C
- Program to print Diamond Pattern in C
- Program to print numeric pattern in C

Advertisements