To rotate a matrix in spiral order, we need to do the following until all the inner matrix and the outer matrix are covered −Step1 − Move elements of top rowStep2 − Move elements of last columnStep3 − Move elements of bottom rowStep4 − Move elements of first columnStep5 − Repeat above steps for inner ring while there is an inner matrix
using System; namespace CsharpCode_ConsoleApplication{ public class Matrix{ public void PrintMatrixInSpiralOrder(int m, int n, int[,] a){ int i, k = 0, l = 0; while (k < m && l < n){ for (i = l; i < n; ++i){ Console.Write(a[k, i] + " "); } k++; for (i = k; i < m; ++i){ Console.Write(a[i, n - 1] + " "); } n--; if (k < m){ for (i = n - 1; i >= l; --i){ Console.Write(a[m - 1, i] + " "); } m--; } if (l < n){ for (i = m - 1; i >= k; --i){ Console.Write(a[i, l] + " "); } l++; } } } } class CsharpCode_Program{ static void Main(string[] args){ Matrix m = new Matrix(); int R = 3; int C = 6; int[,] aa = { { 1, 2, 3, 4, 5, 6 }, { 7, 8, 9, 10, 11, 12 }, { 13, 14, 15, 16, 17, 18 } }; m.PrintMatrixInSpiralOrder(R, C, aa); } } }