Print matrix in spiral form – C programming Tutorial

In this c programming tutorial we are going to see how to print elements of given matrix in spiral form. Consider we have a 2D array as input

        1  2  3  4

        5  6  7  8

        9 10 11 12

       13 14 15 16

Then our output should be as follows:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

To get such output we need to traverse the matrix in spiral manner as show in diagram. To do this we will be using five variables k, l, m, n and i.

  • m for ending row index
  • k for starting row index
  • l for starting column index
  • n for ending column index 
  • i as iterator


Now note that for traversing matrix elements in spiral form we need to ignore row or column which is already printed, that’s what we did in the first three loops inside while statement. Following is the complete c programming code for printing matrix elements in spiral form.

C Program Code :


c programming tutorial

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.