# Print Matrix in Spiral Order

“** Print Matrix in Spiral Order**” is a standard interview problem based on matrix data structure which have been asked in many technical and algorithmic interviews.

Here, we are given a matrix of size N*M and our task is to print the following matrix in spiral Order.

**Example:**

N=3, M=3 1 2 3 4 5 6 7 8 9INPUT:1 2 3 6 9 8 7 4 5OUTPUT:N=4, M=3 1 2 3 4 5 6 7 8 9 10 11 12INPUT:1 2 3 6 9 12 11 10 7 4 5 8OUTPUT:

The steps required to print the matrix in spiral form is as follows:

- Four for loops will be used to print the matrix in spiral form:
- First “for” loop will print row elements from left to right.
- Second “for” loop will print column elements from top to bottom.
- Third “for” loop will print row elements from right to left.
- Fourth “for” loop will print column elements from bottom to top.

The following four “for” loops will work under the constraints of:

1.Starting row index.

2. Ending row index.

3. Starting column index.

4. Ending column index.

Initially, outer boundary elements will be printed, then constraints of point2 will be updated for inner boundary elements, in that way, the spiral printing will be done.

**C++ Program to print matrix in spiral form is as follows:**

#include<bits/stdc++.h> using namespace std; int main() { // Scan Dimensions of the Matrix int row,col; cout<<"Enter the dimensions of the matrix:"; cin>>row>>col; //Initialize a Matrix int matrix[row][col]; //Scan The Matrix cout<<"Enter the Elements of the matrix:"; for(int i = 0 ; i < row ; i++) for(int j = 0 ; j < col ; j++) cin>>matrix[i][j]; // Printing Matrix in Spiral Form int startCol = 0; int endCol=col-1; int startRow=0; int endRow=row-1; int i,j; cout<<"Matrix in Spiral Form is:\n"; while(startCol<=endCol&&startRow<=endRow) { // Print the first row for(j=startCol;j<=endCol;j++) { cout<<matrix[startRow][j]<<" "; } startRow++; // Print the last column for(i=startRow;i<=endRow;i++) { cout<<matrix[i][endCol]<<" "; } endCol--; if(startRow<=endRow) { // Print last Row, if exist for(j=endCol;j>=startCol;j--) { cout<<matrix[endRow][j]<<" "; } } endRow--; if(startCol<=endCol) { // Print first col,if exist for(i=endRow;i>=startRow;i--) { cout<<matrix[i][startCol]<<" "; } } startCol++; } return 0; }

Enter the dimensions of the matrix: 4 4 Enter the Elements of the matrix: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Matrix in Spiral Form is: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10OUTPUT:

**Related Posts:**

**Program to print matrix in zig-zag order.****Program to sort a matrix.****Program to Add two Matrix.****Program to Transpose a Matrix.****Program to Multiply Two Matrix.****Program to check Identity Matrix.****Program to check Idempotent Matrix.****Program to check Involuntary Matrix.****Program to print matrix in zig-zag order.****Program to find first repeating element of an array.****Program to merge two sorted arrays.****Program to find missing number in an array.****Program to sort if array is sorted.****Program to print Alternate Elements of an Array.****Program to swap kth element from beginning to kth element from end in an Array.****Program to print all possible subarrays of the given array.****Program to print kth smallest and kth largest element of an Array.****Program to find equilibrium index of an Array.****Program to find majority element of an Array.****Program to find mean of the Array.****Program to sort an Array of 0s and 1s.**