# Spiral Matrix Printing | Print the elements of a matrix in spiral form

Program to print the elements of a 2d array in the form of a matrix in spiral form is discussed here. The below images gives you an idea of spiral order matrix printing.

Test case:

Input: 3 6 //matrix size

1     2     3     4     5     6
7     8     9    10   11   12
13  14   15   16   17   18

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

## The algorithm to print matrix in spiral form

In order to print a matrix in spiral form, you need to follow the below approach.

• Left to right (first row)
• Top to bottom (Last column)
• Right to left (last row)
• Bottom to top (First column)

## Priniting Matrix in spiral form

#include <stdlib.h>
#include <stdio.h>
int main()
{
int n,m;
printf(“\nInput the number of rows : “);
scanf(“%d”,&m);
printf(“\nInput the number of columns : “);
scanf(“%d”,&n);
int arr[m][n];
int i,j;
printf(“\nInput the matrix \n”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
scanf(“%d”,&arr[i][j]);
}
}
printf(“\nThe Matrix is \n”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
printf(“%d “,arr[i][j]);
}
printf(“\n”);
}
printf(“\nSpiral Matrix : “);
int k = 0, l = 0;
while(k < m && l < n)
{
for(i = l; i < n; i++)
{
printf(“%d “,arr[k][i]);
}
k++;
for(i = k; i < m; i++)
{
printf(“%d “,arr[i][n-1]);
}
n–;
if(k < m)
{
for(i = n – 1; i >= 0; –i)
{
printf(“%d “,arr[m-1][i]);
}
m–;
}
if(l < n)
{
for(i = m – 1; i >= k; i–)
{
printf(“%d “,arr[i][l]);
}
l++;
}
}
return 0;
}

#include <iostream>
using namespace std;
int main()
{
int n,m;
cout << “\nInput the number of rows : “;
cin >> m;
cout << “\nInput the number of columns : “;
cin >> n;
int arr[m][n];
int i,j;
cout << “\nInput the matrix \n”;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
cin >> arr[i][j];
}
}
cout << “\nThe Matrix is \n”;
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
cout << arr[i][j] << ” “;
}
cout << endl;
}
cout << “\nSpiral Matrix : “;
int k = 0, l = 0;
while(k < m && l < n)
{
for(i = l; i < n; i++)
{
cout << arr[k][i] << ” “;
}
k++;
for(i = k; i < m; i++)
{
cout << arr[i][n-1] << ” “;
}
n–;
if(k < m)
{
for(i = n – 1; i >= 0; –i)
{
cout << arr[m-1][i] << ” “;
}
m–;
}
if(l < n)
{
for(i = m – 1; i >= k; i–)
{
cout << arr[i][l] << ” “;
}
l++;
}
}
cout << endl;
return 0;
}

import java.util.*;
public class Main
{
public static void main(String[] args)
{

int n,m;
Scanner sc = new Scanner(System.in);
System.out.print(“\nInput the number of rows : “);
m = sc.nextInt();
System.out.print(“\nInput the number of columns : “);
n = sc.nextInt();
int[][] arr = new int[m][n];
int i,j;
System.out.println(“\nInput the matrix \n”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
arr[i][j] = sc.nextInt();
}
}
System.out.println(“\nThe Matrix is \n”);
for(i = 0; i < m; i++)
{
for(j = 0; j < n; j++)
{
System.out.print(arr[i][j] + ” “);
}
System.out.println();
}
System.out.print(“\nSpiral Matrix : “);
int k = 0, l = 0;
while(k < m && l < n)
{
for(i = l; i < n; i++)
{
System.out.print(arr[k][i] + ” “);
}
k++;
for(i = k; i < m; i++)
{
System.out.print(arr[i][n-1] + ” “);
}
n–;
if(k < m)
{
for(i = n – 1; i >= 0; –i)
{
System.out.print(arr[m-1][i] + ” “);
}
m–;
}
if(l < n)
{
for(i = m – 1; i >= k; i–)
{
System.out.print(arr[i][l] + ” “);
}
l++;
}
}
System.out.println();
}
}

m = int(input(‘Number of rows : ‘))
n = int(input(‘Number of columns : ‘))
arr = []
# initialize the number of rows
for i in range(0,m):
arr += [0]
# initialize the matrix
for i in range (0,m):
arr[i] = [0]*n
for i in range (0,m):
for j in range (0,n):
arr[i][j] = int(input())
print (“\nThe Matrix is”)
for i in range (0,m):
for j in range (0,n):
print(arr[i][j], end = ” “)
print(“\n”)
k = 0; l = 0
print(“Spiral Matrix : “,end = ” “)
while (k < m and l < n):
for i in range(l, n):
print(arr[k][i], end = ” “)
k += 1
for i in range(k, m):
print(arr[i][n – 1], end = ” “)
n -= 1
if ( k < m):
for i in range(n – 1, (l – 1), -1) :
print(arr[m – 1][i], end = ” “)
m -= 1
if (l < n):
for i in range(m – 1, k – 1, -1):
print(arr[i][l], end = ” “)
l += 1

The complexity of spiral matrix printing: O(M*N)