Rotate a Matrix by 180 degree

Given a square matrix the task is that we turn it by 180 degrees in anti-clockwise direction without using any extra space.
Examples :

Input :  1  2  3
         4  5  6
         7  8  9
Output : 9 8 7 
         6 5 4 
         3 2 1

Input :  1 2 3 4 
         5 6 7 8 
         9 0 1 2 
         3 4 5 6 
Output : 6 5 4 3 
         2 1 0 9 
         8 7 6 5 
         4 3 2 1

Method : 1 (Only prints rotated matrix)
The solution of this problem is that to rotate a matrix by 180 degree we can easily follow that step

Matrix =  a00 a01 a02
          a10 a11 a12
          a20 a21 a22

when we rotate it by 90 degree
then matrix is
Matrix = a02 a12 a22
         a01 a11 a21
         a00 a10 a20
  
when we rotate it by again 90 
degree then matrix is 
Matrix = a22 a21 a20
         a12 a11 a10
         a02 a01 a00 

From the above illustration we get that simply to rotate the matrix by 180 degree then we will have to print given matrix in reverse manner .

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to rotate a matrix by 180 degrees
#include <bits/stdc++.h>
#define N 3
using namespace std;
  
// Function to Rotate the matrix by 180 degree
void rotateMatrix(int mat[][N])
{
    // Simply print from last cell to first cell.
    for (int i = N - 1; i >= 0; i--) {
        for (int j = N - 1; j >= 0; j--)
            printf("%d ", mat[i][j]);
  
        printf("\n");
    }
}
  
// Driven code
int main()
{
    int mat[N][N] = {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 }
    };
  
    rotateMatrix(mat);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to rotate a
// matrix by 180 degrees
import java.util.*;
  
class GFG {
    static int N = 3;
  
    // Function to Rotate the
    // matrix by 180 degree
    static void rotateMatrix(int mat[][])
    {
  
        // Simply print from last
        // cell to first cell.
        for (int i = N - 1; i >= 0; i--) {
            for (int j = N - 1; j >= 0; j--)
                System.out.print(mat[i][j] + " ");
  
            System.out.println();
        }
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        int[][] mat = { { 1, 2, 3 },
                        { 4, 5, 6 },
                        { 7, 8, 9 } };
  
        rotateMatrix(mat);
    }
}
  
// This code is contributed by ChitraNayal

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to 
# rotate a matrix by 
# 180 degrees
N = 3;
  
# Function to Rotate 
# the matrix by 180 degree
def rotateMatrix(mat):
      
    # Simply print from
    # last cell to first cell.
    i = N - 1
    while(i >= 0):
        j = N - 1;
        while(j >= 0):
            print(mat[i][j], end = " ");
            j = j - 1;
        print();
        i = i - 1;
  
# Driven code
mat = [[1, 2, 3],
       [ 4, 5, 6 ],
       [ 7, 8, 9 ]];
rotateMatrix(mat);
  
# This code is contributed 
# by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to rotate a
// matrix by 180 degrees
using System;
  
class GFG {
    static int N = 3;
  
    // Function to Rotate the
    // matrix by 180 degree
    static void rotateMatrix(int[, ] mat)
    {
        // Simply print from last
        // cell to first cell.
        for (int i = N - 1; i >= 0; i--) {
            for (int j = N - 1; j >= 0; j--)
                Console.Write(mat[i, j] + " ");
  
            Console.WriteLine();
        }
    }
  
    // Driver Code
    static public void Main()
    {
        int[, ] mat = { { 1, 2, 3 },
                        { 4, 5, 6 },
                        { 7, 8, 9 } };
  
        rotateMatrix(mat);
    }
}
  
// This code is contributed by aj_36

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to rotate 
// a matrix by 180 degree
$N = 3;
  
// Function to Rotate the
// matrix by 180 degree
function rotateMatrix($mat)
{
    global $N;
      
    // Simply print from 
    // last cell to first cell.
    for ($i = $N - 1; $i >= 0; $i--) 
    {
        for ($j = $N - 1; $j >= 0; $j--) 
            echo $mat[$i][$j], " ";
          
    echo "\n";
    }
}
  
// Driver Code
$mat = array(array(1, 2, 3),
             array(4, 5, 6),
             array(7, 8, 9));
  
rotateMatrix($mat);
  
// This code is contributed by ajit
?>

chevron_right