Rotate a matrix by 90 degree in clockwise direction without using any extra space |  ; rotate function ; first rotation with respect to main diagonal ; Second rotation with respect to middle column ; to print matrix ; Driver code
using System ; using System . Collections . Generic ; public class GFG { static void rotate ( int [ , ] arr ) { int n = arr . GetLength ( 0 ) ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < i ; ++ j ) { int temp = arr [ i , j ] ; arr [ i , j ] = arr [ j , i ] ; arr [ j , i ] = temp ; } } for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < n / 2 ; ++ j ) { int temp = arr [ i , j ] ; arr [ i , j ] = arr [ i , n - j - 1 ] ; arr [ i , n - j - 1 ] = temp ; } } } static void printMatrix ( int [ , ] arr ) { int n = arr . GetLength ( 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) Console . Write ( arr [ i , j ] + " ▁ " ) ; Console . WriteLine ( ) ; } } public static void Main ( String [ ] args ) { int [ , ] arr = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; rotate ( arr ) ; printMatrix ( arr ) ; } }