Printing Matrix in Snake Number Pattern
In the previous article, we have discussed Java Program to Print Square with Right Rotate N-Numbers by 1 Pattern
In this article we will see how to print matrix in snake number pattern.
When input is like this 5*5 matrix {{ 1, 2, 3, 4, 5 } { 6, 7, 8, 9, 10 } { 11, 12, 13, 14, 15 } { 16, 17, 18, 19, 20 } { 21, 22, 23, 24, 25 }}; Output is like 5 4 3 2 1 6 7 8 9 10 15 14 13 12 11 16 17 18 19 20 25 24 23 22 21
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Approach :
- Enter the dimension of the matrix and store in an integer variable
size
. - Declare the
n*n matrix
according to the dimension. - Declare the value of the matrix.
- Take a loop to traverse form top right corner form right to left horizontally .
- For each iteration check if row value is even the traverse left to right else right to left .
Java Code to Print Matrix in Snake Number Pattern:
import java.io.*; class Main { public static void main(String[] args) { // taking the dimension of the square of the matrix as size int size = 5,numberOfRows , numberOfColumns ; // entering the values of n*n matrix int[][] mat = {{ 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 },{ 11, 12, 13, 14, 15 },{ 16, 17, 18, 19, 20 }, { 21, 22, 23, 24, 25 }}; //loop to traverse form top right corner form right to left horizontally for ( numberOfRows = 0; numberOfRows < size; numberOfRows++) { // if row value is odd then move right to left if (numberOfRows % 2 == 1) { for ( numberOfColumns = 0; numberOfColumns < size; numberOfColumns++) { //printing coordinate values System.out.print(Integer.toString(mat[numberOfRows][numberOfColumns]) + " "); } } // else then move left to right else { for ( numberOfColumns = size - 1; numberOfColumns >= 0; numberOfColumns--) { //printing coordinate values System.out.print(Integer.toString(mat[numberOfRows][numberOfColumns]) + " "); } } } } }
Output : 5 4 3 2 1 6 7 8 9 10 15 14 13 12 11 16 17 18 19 20 25 24 23 22 21
C Code to Print Matrix in Snake Number Pattern:
#include <stdio.h> #include <stdlib.h> int main() { int size = 5,numberOfRows , numberOfColumns ; int mat[5][5] = {{ 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 },{ 11, 12, 13, 14, 15 },{ 16, 17, 18, 19, 20 }, { 21, 22, 23, 24, 25 }}; for (int numberOfRows = 0; numberOfRows < size; numberOfRows++) { if (numberOfRows%2 == 1) for (int numberOfColumns = 0; numberOfColumns < size; numberOfColumns++) printf("%d ", mat[numberOfRows][numberOfColumns]); else for (int numberOfColumns = size - 1; numberOfColumns >= 0; numberOfColumns--) printf("%d ", mat[numberOfRows][numberOfColumns]); } return 0; }
Output : 5 4 3 2 1 6 7 8 9 10 15 14 13 12 11 16 17 18 19 20 25 24 23 22 21
Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.
Related Java Number Pattern Programs: