Print a 2D Array or Matrix using single loop Last Updated : 08 Jun, 2021 Comments Improve Suggest changes Like Article Like Report Given a matrix mat[][] of N * M dimensions, the task is to print the elements of the matrix using a single for loop. Examples: Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}Output: 1 2 3 4 5 6 7 8 9 Input: mat[][] = {{7, 9}, {10, 34}, {12, 15}}Output: 7 9 10 34 12 15 Approach: To traverse the given matrix using a single loop, observe that there are only N * M elements. Therefore, the idea is to use modulus and division to switch the rows and columns while iterating a single loop over the range [0, N * M]. Follow the steps below to solve the given problem: Iterate a loop over the range [0, N * M] using the variable i.At each iteration, find the index of the current row and column as row = i / M and column = i % M respectively.In the above steps, print the value of mat[row][column] to get the value of the matrix at that index. Below is the implementation of the above approach: C++ // C++ program for the above approach #include <iostream> using namespace std; // Function to print the element // of 2D matrix using single loop void print2DMatrix(int arr[][3], int rows, int columns) { // Iterate over the range // [0, rows*columns] for(int i = 0; i < rows * columns; i++) { // Find row and column index int row = i / columns; int col = i % columns; // Print the element at // current index cout << arr[row][col] << " "; } } // Driver Code int main() { // Given matrix mat[][] int mat[][3] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // Dimensions of the matrix int N = sizeof(mat) / sizeof(mat[0]); int M = sizeof(mat[0]) / sizeof(mat[0][0]); // Function Call print2DMatrix(mat, N, M); return 0; } // This code is contributed by akhilsaini Java // Java Program for the above approach import java.io.*; class GFG { // Function to print the element // of 2D matrix using single loop public static void print2DMatrix( int arr[][], int rows, int columns) { // Iterate over the range // [0, rows*columns] for (int i = 0; i < rows * columns; i++) { // Find row and column index int row = i / columns; int col = i % columns; // Print the element at // current index System.out.print( arr[row][col] + " "); } } // Driver Code public static void main(String[] args) { // Given matrix mat[][] int[][] mat = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // Dimensions of the matrix int N = mat.length; int M = mat[0].length; // Function Call print2DMatrix(mat, N, M); } } Python3 # Python3 program for the above approach # Function to print the element # of 2D matrix using single loop def print2DMatrix(arr, rows, columns): # Iterate over the range # [0, rows*columns] for i in range(0, rows * columns): # Find row and column index row = i // columns col = i % columns # Print the element at # current index print(arr[row][col], end = ' ') # Driver Code if __name__ == '__main__': # Given matrix mat[][] mat = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] # Dimensions of the matrix N = len(mat) M = len(mat[0]) # Function Call print2DMatrix(mat, N, M) # This code is contributed by akhilsaini C# // C# program for the above approach using System; class GFG{ // Function to print the element // of 2D matrix using single loop public static void print2DMatrix(int[, ] arr, int rows, int columns) { // Iterate over the range // [0, rows*columns] for(int i = 0; i < rows * columns; i++) { // Find row and column index int row = i / columns; int col = i % columns; // Print the element at // current index Console.Write(arr[row, col] + " "); } } // Driver Code public static void Main() { // Given matrix mat[][] int[, ] mat = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; // Dimensions of the matrix int N = mat.GetLength(0); int M = mat.GetLength(1); // Function Call print2DMatrix(mat, N, M); } } // This code is contributed by akhilsaini JavaScript <script> // JavaScript program for the above approach // Function to print the element // of 2D matrix using single loop function print2DMatrix(arr,rows,columns) { // Iterate over the range // [0, rows*columns] for(let i = 0; i < rows * columns; i++) { // Find row and column index let row = parseInt(i / columns, 10); let col = i % columns; // Print the element at // current index document.write(arr[row][col] + " "); } } // Given matrix mat[][] let mat = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]; // Dimensions of the matrix let N = mat.length; let M = mat[0].length; // Function Call print2DMatrix(mat, N, M); </script> Output: 1 2 3 4 5 6 7 8 9 Time Complexity: O(N * M)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Print a 2D Array or Matrix using single loop Anonymous Improve Article Tags : Interview Experiences Algorithms Matrix Computer Science Fundamentals DSA Arrays Experiences interview-preparation +4 More Practice Tags : AlgorithmsArraysMatrix Similar Reads Program to print Lower triangular and Upper triangular matrix of an array Prerequisite - Multidimensional Arrays in C / C++Given a two dimensional array, Write a program to print lower triangular matrix and upper triangular matrix. Lower triangular matrix is a matrix which contains elements below principal diagonal including principal diagonal elements and rest of the ele 9 min read Emulating a 2-d array using 1-d array How to convert a 2-d array of size (m x n) into 1-d array and how to store the element at position [i, j] of 2-d array in 1-d array? Clearly, the size of 1-d array is the number of elements in 2-d array i.e. m x n). If the elements in the 2-d array are stored in row-major order. Then, the element at 5 min read Program to print the Diagonals of a Matrix Given a 2D square matrix, print the Principal and Secondary diagonals. Examples : Input: 41 2 3 44 3 2 17 8 9 66 5 4 3Output:Principal Diagonal: 1, 3, 9, 3Secondary Diagonal: 4, 2, 8, 6Input:31 1 11 1 11 1 1Output:Principal Diagonal: 1, 1, 1Secondary Diagonal: 1, 1, 1For example, consider the follow 14 min read Print matrix in zig-zag fashion Given a matrix of 2D array of n rows and m columns. Print this matrix in ZIG-ZAG fashion as shown in figure. Example: Input: {{1, 2, 3}{4, 5, 6}{7, 8, 9}}Output: 1 2 4 7 5 3 6 8 9Input : [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]Output:: 1 2 5 9 6 3 4 7 10 13 14 11 8 12 15 16Thi 10 min read Print a given matrix in zigzag form Given a 2D array, print it in zigzag form. Examples : Input : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Output : 1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16 Input : 10 24 32 50 6 17 99 10 11 Output : 10 24 32 17 6 50 99 10 11 CPP // C++ program to print // matrix in zig-zag form #inc 7 min read Print a matrix in Reverse Wave Form Given a matrix, print it in Reverse Wave Form. Examples : Input : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output : 4 8 12 16 15 11 7 3 2 6 10 14 13 9 5 1 Input : 1 9 4 10 3 6 90 11 2 30 85 72 6 31 99 15 Output : 10 11 72 15 99 85 90 4 9 6 30 31 6 2 3 1 Approach :To get the reverse wave form for a giv 8 min read Javascript Program to Print matrix in snake pattern Given 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 2 min read Print matrix in snake pattern 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] 4 min read Print the matrix diagonally downwards Given a matrix of size n*n, print the matrix in the following pattern. Output: 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 16 Examples: Input :matrix[2][2]= { {1, 2}, {3, 4} } Output : 1 2 3 4 Input :matrix[3][3]= { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} } Output : 1 2 4 3 5 7 6 8 9 Implementation: Following is th 5 min read Print matrix in diagonal pattern Given a matrix of n*n size, the task is to print its elements in a diagonal pattern. Input : mat[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Output : 1 2 4 7 5 3 6 8 9. Explanation: Start from 1 Then from upward to downward diagonally i.e. 2 and 4 Then from downward to upward diagonally i.e 7, 5, 3 Th 14 min read Like