#include <stdio.h> 

void spiralOrder(int matrix[][4], int n) { 

 int top = 0, boƩom = n - 1, left = 0, right = n - 1; 

 while (top <= bottom && left <= right) {

 for (int i = left; i <= right; i++) {
 printf("%d ", matrix[top][i]);

 } top++; 

 for (int i = top; i <= bottom; i++) {

 printf("%d ", matrix[i][right]);

 } 

 right--; 

 if (top <= bottom) {

 for (int i = right; i >= left; i--) { 

 printf("%d ", matrix[bottom][i]);

 } 
 bottom--; 

 } 

 if (left <= right) {

 for (int i = bottom; i >= top; i--) { 

 printf("%d ", matrix[i][left]);

 } 

 left++;

 } 

 } 

} 

int main() { 

 int matrix[4][4] = {{1, 2, 3, 4}, 

 {5, 6, 7, 8}, 

 {9, 10, 11, 12}, 

 {13, 14, 15, 16}}; 

 int n = 4; 

 printf("Spiral Order Matrix:\n"); 
    spiralOrder(matrix, n); 

 return 0; 

} 

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: