Print a given matrix in spiral form | AMCAT Automata Questions - 4

Print a given matrix in spiral form. 

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

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

Input:
        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





#include <stdio.h>
int main(void) {
int n[4][4] = {{1,2,3,4},
               {5,6,7,8},
               {9,10,11,12},
               {13,14,15,16}};

 for(int i=0;i<4;i++)
 {
 if(i%2==0){
   for(int j=0;j<4;j++){
  printf("%d ",n[i][j]);
   }
 }
 else
 {
   for(int j=3;j>=0;j--)
   {
  printf("%d ",n[i][j]);
   }
  }
 }
 return 0;
}





Check Out More Questions from AMCAT Automata Test : Click here

Post a Comment

0 Comments