// Simple C# program to find mirror of // matrix across diagonal. using System; using System.Collections.Generic; class GFG { static int MAX = 100; static void imageSwap(int [,]mat, int n) { // for diagonal which start from at // first row of matrix int row = 0; // traverse all top right diagonal for (int j = 0; j < n; j++) { // here we use stack for reversing // the element of diagonal Stack<int> s = new Stack<int>(); int i = row, k = j; while (i < n && k >= 0) { s.Push(mat[i++,k--]); } // push all element back to matrix // in reverse order i = row; k = j; while (i < n && k >= 0) { mat[i++,k--] = s.Peek(); s.Pop(); } } // do the same process for all the // diagonal which start from last // column int column = n - 1; for (int j = 1; j < n; j++) { // here we use stack for reversing // the elements of diagonal Stack<int> s = new Stack<int>(); int i = j, k = column; while (i < n && k >= 0) { s.Push(mat[i++,k--]); } // push all element back to matrix // in reverse order i = j; k = column; while (i < n && k >= 0) { mat[i++,k--] = s.Peek(); s.Pop(); } } } // Utility function to print a matrix static void printMatrix(int [,]mat, int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { Console.Write(mat[i,j] + " "); } Console.WriteLine(""); } } // Driver code public static void Main(String[] args) { int [,]mat = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; int n = 4; imageSwap(mat, n); printMatrix(mat, n); } } /* This code contributed by PrinciRaj1992 */