fork download
  1. #include<stdio.h>
  2.  
  3. int main(){
  4. int n;
  5. int a[n][n];
  6. printf("Enter the order of amtrix\n");
  7. scanf("%d",&n);
  8. printf("Enter all the elements of matrix here\n");
  9. for(int q=0;q<n;q++){
  10. for(int w=0;w<n;w++){
  11. scanf("%d ",&a[q][w]);
  12.  
  13. }
  14. }
  15. printf("The elements of the matrix are printed below according to spiral order");
  16. for(int e=0;e<n;e++){
  17. printf("%d ",a[0][e]);
  18. }
  19. int h=0;
  20. int k=n-1;
  21.  
  22. if(n%2==0){
  23. for(int i=n-1;i>1;i-=2){
  24. for(int p=0;p<i;p++){
  25. h+=1;
  26. printf("%d ",a[h][k]);
  27.  
  28. }
  29. for(int p=0;p<i;p++){
  30. k-=1;
  31. printf("%d ",a[h][k]);
  32. }
  33.  
  34. for(int p=0;p<i-1;p++){
  35. h-=1;
  36. printf("%d ",a[h][k]);
  37.  
  38. }
  39. for(int p=0;p<i-1;p++){
  40. k+=1;
  41. printf("%d ",a[h][k]);
  42. }
  43. }
  44. printf("%d",a[h+1][k]);
  45. printf("%d",a[h][k-1]);
  46.  
  47. }else{
  48. for(int i=n-1;i>0;i-=2){
  49. for(int p=0;p<i;p++){
  50. h+=1;
  51. printf("%d ",a[h][k]);
  52.  
  53. }
  54. for(int p=0;p<i;p++){
  55. k-=1;
  56. printf("%d ",a[h][k]);
  57. }
  58.  
  59. for(int p=0;p<i-1;p++){
  60. h-=1;
  61. printf("%d ",a[h][k]);
  62.  
  63. }
  64. for(int p=0;p<i-1;p++){
  65. k+=1;
  66. printf("%d ",a[h][k]);
  67. }
  68. }
  69. }
  70. return 0;
  71. }
  72.  
  73.  
  74.  
Success #stdin #stdout 0.01s 5320KB
stdin
3
stdout
Enter the order of amtrix
Enter all the elements of matrix here
The elements of the matrix are printed below according to spiral order0 0 0 0 0 0 0 0 0