Print a Matrix in Snake Pattern

PHOTO EMBED

Tue Feb 08 2022 07:19:12 GMT+0000 (Coordinated Universal Time)

Saved by @Uttam #java #gfg #geeksforgeeks #lecture #2d #array #matrix #snakepattern

import java.util.*;
import java.io.*;

class GFG 
{ 
	static int R = 4, C = 4;
	static void printSnake(int mat[][])
	{
		for(int i = 0; i < R; i++)
		{
			if(i % 2 == 0)
			{
				for(int j = 0; j < C; j++)
					System.out.print(mat[i][j] + " ");
			}
			else
			{
				for(int j = C - 1; j >= 0; j--)
					System.out.print(mat[i][j] + " ");
			}
		}
	}

	public static void main(String args[]) 
    {
        int arr[][] = {{1, 2, 3, 4},
    				   {5, 6, 7, 8},
    				   {9, 10, 11, 12},
    				   {13, 14, 15, 16}};

    	printSnake(arr);
    } 
}
content_copyCOPY

Input : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output : 1 2 3 4 8 7 6 5 9 10 11 12 16 15 14 13 Input : 1 2 3 4 5 6 7 8 9 10 11 12 Output : 1 2 3 4 8 7 6 5 9 10 11 12