Skip to main content

Rotate a Matrix by 180 degree

Rotating a matrix diagonally by 180 degrees means flipping the elements of the matrix along its diagonal axis. This means that the element at position (i,j) in the original matrix would now be at position (n-i-1,n-j-1) in the rotated matrix, where n is the size of the matrix.

Code Solution

//C Program 
//Rotate a Matrix by 180 degree
#include<stdio.h>

#define ROW 5
#define COL 5
//rotate matrix elements in 180 degree
void rotate_matrix(int matrix[][COL])
{
  int temp=0;

  int row=ROW-1,col=COL-1;

  int row_size = (ROW/2);

  int col_size = COL;

  if(ROW%2==1)
  { 
    //When in case number of rows are Odd size
    row_size = (ROW/2)+1;
  }
  for (int i = 0; i < row_size; ++i)
  {
    if(ROW/2 == i)
    {
      //When in case number of rows are Odd size
      //In This case reverse middle row element
      col_size = COL/2;
    }
    else
    {
      col_size = COL;
    }
    for (int j = 0; j < col_size; ++j)
    {
      //Swap element
      temp=matrix[i][j];
      matrix[i][j]=matrix[row-i][col-j];
      matrix[row-i][col-j]=temp;
    }
  }
}
//Display matrix
void show_data(int matrix[][COL])
{
  for (int i = 0; i < ROW; ++i)
  {
    for (int j = 0; j < COL; ++j)
    {
      printf("%3d",matrix[i][j] );
    }
    printf("\n");
  }
  printf("\n");
}
int main()
{
  int matrix[][COL]={ 
    {1,  2,  3,  4 , 5}, 
    {6,  7,  8,  9 ,10},
    {11, 12, 13, 14, 15} ,
    {16, 17, 18, 19, 20},  
    {21, 22, 23, 24, 25}  
  }; 
  show_data(matrix);
  rotate_matrix(matrix);
  show_data(matrix);
  return 0;
}


Output

  1  2  3  4  5
  6  7  8  9 10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25

 25 24 23 22 21
 20 19 18 17 16
 15 14 13 12 11
 10  9  8  7  6
  5  4  3  2  1
/*
  C++ Program
  Rotate a Matrix by 180 degree
*/
#include<iostream>
#define ROW 5
#define COL 5
using namespace std;


class MyMatrix {
	public:
		int rows;
	int cols;
	MyMatrix() {
		//Get matrix size
		this->rows = ROW;
		this->cols = COL;
	}
	//rotate matrix elements in 180 degree
	void rotate_matrix(int matrix[][COL]) {
		int temp = 0;
		int row = this->rows - 1, col = this->cols - 1;
		int row_size = (this->rows / 2);
		int col_size = this->cols;
		if (this->rows % 2 == 1) {
			//When in case number of rows are Odd size
			row_size = (this->rows / 2) + 1;
		}
		for (int i = 0; i < row_size; ++i) {
			if (this->rows / 2 == i) {
				//When in case number of rows are Odd size
				//In This case reverse middle row element
				col_size = this->cols / 2;
			} else {
				col_size = this->cols;
			}
			for (int j = 0; j < col_size; ++j) {
				//Swap element
				temp = matrix[i][j];
				matrix[i][j] = matrix[row - i][col - j];
				matrix[row - i][col - j] = temp;
			}
		}
	}
	//Display matrix
	void show_data(int matrix[][COL]) {
		for (int i = 0; i < this->rows; ++i) {
			for (int j = 0; j < this->cols; ++j) {
				cout << " " << matrix[i][j];
			}
			cout << "\n";
		}
		cout << "\n";
	}
};
int main() {
	int matrix[][COL] = {
		{
			1,
			2,
			3,
			4,
			5
		},
		{
			6,
			7,
			8,
			9,
			10
		},
		{
			11,
			12,
			13,
			14,
			15
		},
		{
			16,
			17,
			18,
			19,
			20
		},
		{
			21,
			22,
			23,
			24,
			25
		}
	};
	MyMatrix obj ;
	obj.show_data(matrix);
	obj.rotate_matrix(matrix);
	obj.show_data(matrix);
	return 0;
}

Output

 1 2 3 4 5
 6 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25

 25 24 23 22 21
 20 19 18 17 16
 15 14 13 12 11
 10 9 8 7 6
 5 4 3 2 1
/*
  Java Program
  Rotate a Matrix by 180 degree
*/

public class MyMatrix {
  int rows;
  int cols;
  MyMatrix(int[][] matrix) {
    //Get matrix size
    this.rows = matrix.length;
    this.cols = matrix[0].length;
  }
  //rotate matrix elements in 180 degree
  void rotate_matrix(int [][]matrix)
  {
    int temp=0;

    int row=this.rows-1,col=this.cols-1;

    int row_size = (this.rows/2);

    int col_size = this.cols;

    if(this.rows%2==1)
    { 
      //When in case number of rows are Odd size
      row_size = (this.rows/2)+1;
    }
    for (int i = 0; i < row_size; ++i)
    {
      if(this.rows/2 == i)
      {
        //When in case number of rows are Odd size
        //In This case reverse middle row element
        col_size = this.cols/2;
      }
      else
      {
        col_size = this.cols;
      }
      for (int j = 0; j < col_size; ++j)
      {
        //Swap element
        temp=matrix[i][j];
        matrix[i][j]=matrix[row-i][col-j];
        matrix[row-i][col-j]=temp;
      }
    }
  }
  //Display matrix
  void show_data(int [][]matrix)
  {
    for (int i = 0; i < this.rows; ++i)
    {
      for (int j = 0; j < this.cols; ++j)
      {
        System.out.print("  "+matrix[i][j] );
      }
      System.out.print("\n");
    }
    System.out.print("\n");
  }
  public static void main(String[] args) {
    int[][] matrix = { 
      {1,  2,  3,  4 , 5}, 
      {6,  7,  8,  9 ,10},
      {11, 12, 13, 14, 15} ,
      {16, 17, 18, 19, 20},  
      {21, 22, 23, 24, 25}  
    }; 
    MyMatrix obj = new MyMatrix(matrix);
    obj.show_data(matrix);
    obj.rotate_matrix(matrix);
    obj.show_data(matrix);
  }
}

Output

 1 2 3 4 5
 6 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25

 25 24 23 22 21
 20 19 18 17 16
 15 14 13 12 11
 10 9 8 7 6
 5 4 3 2 1
/*
  C# Program
  Rotate a Matrix by 180 degree
*/
using System;

public class MyMatrix {
	int rows;
	int cols;
	MyMatrix(int[,] matrix) {
		//Get matrix size
		this.rows = matrix.GetLength(0);
		this.cols = matrix.GetLength(1);
	}
	//rotate matrix elements in 180 degree
	void rotate_matrix(int[,] matrix) {
		int temp = 0;
		int row = this.rows - 1, col = this.cols - 1;
		int row_size = (this.rows / 2);
		int col_size = this.cols;
		if (this.rows % 2 == 1) {
			//When in case number of rows are Odd size
			row_size = (this.rows / 2) + 1;
		}
		for (int i = 0; i < row_size; ++i) {
			if (this.rows / 2 == i) {
				//When in case number of rows are Odd size
				//In This case reverse middle row element
				col_size = this.cols / 2;
			} else {
				col_size = this.cols;
			}
			for (int j = 0; j < col_size; ++j) {
				//Swap element
				temp = matrix[i,j];
				matrix[i,j] = matrix[row - i,col - j];
				matrix[row - i,col - j] = temp;
			}
		}
	}
	//Display matrix
	void show_data(int[,] matrix) {
		for (int i = 0; i < this.rows; ++i) {
			for (int j = 0; j < this.cols; ++j) {
				Console.Write(" " + matrix[i,j]);
			}
			Console.Write("\n");
		}
		Console.Write("\n");
	}
	public static void Main(String[] args) {
		int[,] matrix = {
			{
				1,
				2,
				3,
				4,
				5
			},
			{
				6,
				7,
				8,
				9,
				10
			},
			{
				11,
				12,
				13,
				14,
				15
			},
			{
				16,
				17,
				18,
				19,
				20
			},
			{
				21,
				22,
				23,
				24,
				25
			}
		};
		MyMatrix obj = new MyMatrix(matrix);
		obj.show_data(matrix);
		obj.rotate_matrix(matrix);
		obj.show_data(matrix);
	}
}

Output

 1 2 3 4 5
 6 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25

 25 24 23 22 21
 20 19 18 17 16
 15 14 13 12 11
 10 9 8 7 6
 5 4 3 2 1
<?php
/*
  Php Program
  Rotate a Matrix by 180 degree
*/
class MyMatrix {
	public $rows;
	public $cols;

	function __construct($matrix) {
		//Get matrix size
		$this->rows = count($matrix);
		$this->cols = count($matrix[0]);
	}
	//rotate matrix elements in 180 degree
	function rotate_matrix(&$matrix) {
		$temp = 0;
		$row = $this->rows - 1;
		$col = $this->cols - 1;
		$row_size = (intval($this->rows / 2));
		$col_size = $this->cols;
		if ($this->rows % 2 == 1) {
			//When in case number of rows are Odd size
			$row_size = (intval($this->rows / 2)) + 1;
		}
		for ($i = 0; $i < $row_size; ++$i) {
			if (intval($this->rows / 2) == $i) {
				//When in case number of rows are Odd size
				//In This case reverse middle row element
				$col_size = intval($this->cols / 2);
			} else {
				$col_size = $this->cols;
			}
			for ($j = 0; $j < $col_size; ++$j) {
				//Swap element
				$temp = $matrix[$i][$j];
				$matrix[$i][$j] = $matrix[$row - $i][$col - $j];
				$matrix[$row - $i][$col - $j] = $temp;
			}
		}
	}
	//Display matrix
	function show_data($matrix) {
		for ($i = 0; $i < $this->rows; ++$i) {
			for ($j = 0; $j < $this->cols; ++$j) {
				echo(" ". $matrix[$i][$j]);
			}
			echo("\n");
		}
		echo("\n");
	}
}

function main() {
	$matrix = array(
      array(1, 2, 3, 4, 5), 
      array(6, 7, 8, 9, 10), 
      array(11, 12, 13, 14, 15), 
      array(16, 17, 18, 19, 20), 
      array(21, 22, 23, 24, 25));
	$obj = new MyMatrix($matrix);
	$obj->show_data($matrix);
	$obj->rotate_matrix($matrix);
	$obj->show_data($matrix);

}
main();

Output

 1 2 3 4 5
 6 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25

 25 24 23 22 21
 20 19 18 17 16
 15 14 13 12 11
 10 9 8 7 6
 5 4 3 2 1
/*
  Node Js Program
  Rotate a Matrix by 180 degree
*/
class MyMatrix {
	constructor(matrix) {
		//Get matrix size
		this.rows = matrix.length;
		this.cols = matrix[0].length;
	}

	//rotate matrix elements in 180 degree
	rotate_matrix(matrix) {
		var temp = 0;
		var row = this.rows - 1;
		var col = this.cols - 1;
		var row_size = (parseInt(this.rows / 2));
		var col_size = this.cols;
		if (this.rows % 2 == 1) {
			//When in case number of rows are Odd size
			row_size = (parseInt(this.rows / 2)) + 1;
		}

		for (var i = 0; i < row_size; ++i) {
			if (parseInt(this.rows / 2) == i) {
				//When in case number of rows are Odd size
				//In This case reverse middle row element
				col_size = parseInt(this.cols / 2);
			} else {
				col_size = this.cols;
			}

			for (var j = 0; j < col_size; ++j) {
				//Swap element
				temp = matrix[i][j];
				matrix[i][j] = matrix[row - i][col - j];
				matrix[row - i][col - j] = temp;
			}
		}
	}

	//Display matrix
	show_data(matrix) {
		for (var i = 0; i < this.rows; ++i) {
			for (var j = 0; j < this.cols; ++j) {
				process.stdout.write(" " + matrix[i][j]);
			}

			process.stdout.write("\n");
		}

		process.stdout.write("\n");
	}
}

function main(args) {
	var matrix = [
		[1, 2, 3, 4, 5],
		[6, 7, 8, 9, 10],
		[11, 12, 13, 14, 15],
		[16, 17, 18, 19, 20],
		[21, 22, 23, 24, 25]
	];
	var obj = new MyMatrix(matrix);
	obj.show_data(matrix);
	obj.rotate_matrix(matrix);
	obj.show_data(matrix);
}

main();

Output

 1 2 3 4 5
 6 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25

 25 24 23 22 21
 20 19 18 17 16
 15 14 13 12 11
 10 9 8 7 6
 5 4 3 2 1
# Python 3 Program
# Rotate a Matrix by 180 degree
class MyMatrix :
	
	def __init__(self, matrix) :
		# Get matrix size
		self.rows = len(matrix)
		self.cols = len(matrix[0])
	
	def rotate_matrix(self, matrix) :
		temp = 0
		row = self.rows - 1
		col = self.cols - 1
		row_size = (int(self.rows / 2))
		col_size = self.cols
		if (self.rows % 2 == 1) :
			# When in case number of rows are Odd size
			row_size = (int(self.rows / 2)) + 1
		
		i = 0
		while (i < row_size) :
			if (int(self.rows / 2) == i) :
				# When in case number of rows are Odd size
				# In This case reverse middle row element
				col_size = int(self.cols / 2)
			else :
				col_size = self.cols
			
			j = 0
			while (j < col_size) :
				# Swap element
				temp = matrix[i][j]
				matrix[i][j] = matrix[row - i][col - j]
				matrix[row - i][col - j] = temp
				j += 1
			
			i += 1
		
	
	def show_data(self, matrix) :
		i = 0
		while (i < self.rows) :
			j = 0
			while (j < self.cols) :
				print(" ", matrix[i][j], end = "")
				j += 1
			
			print("\n", end = "")
			i += 1
		
		print("\n", end = "")
	

def main() :
	matrix = [
		[1, 2, 3, 4, 5],
		[6, 7, 8, 9, 10],
		[11, 12, 13, 14, 15],
		[16, 17, 18, 19, 20],
		[21, 22, 23, 24, 25]
	]
	obj = MyMatrix(matrix)
	obj.show_data(matrix)
	obj.rotate_matrix(matrix)
	obj.show_data(matrix)


if __name__ == "__main__":
	main()

Output

  1  2  3  4  5
  6  7  8  9  10
  11  12  13  14  15
  16  17  18  19  20
  21  22  23  24  25

  25  24  23  22  21
  20  19  18  17  16
  15  14  13  12  11
  10  9  8  7  6
  5  4  3  2  1
# Ruby Program
# Rotate a Matrix by 180 degree
class MyMatrix 
    # Define the accessor and reader of class MyMatrix
    attr_reader :rows, :cols
    attr_accessor :rows, :cols
	
	def initialize(matrix) 
		# Get matrix size
		self.rows = matrix.length
		self.cols = matrix[0].length
	end
	def rotate_matrix(matrix) 
		temp = 0
		row = self.rows - 1
		col = self.cols - 1
		row_size = (self.rows / 2)
		col_size = self.cols
		if (self.rows % 2 == 1) 
			# When in case number of rows are Odd size
			row_size = (self.rows / 2) + 1
		end
		i = 0
		while (i < row_size) 
			if (self.rows / 2 == i) 
				# When in case number of rows are Odd size
				# In This case reverse middle row element
				col_size = self.cols / 2
			else 
				col_size = self.cols
			end
			j = 0
			while (j < col_size) 
				# Swap element
				temp = matrix[i][j]
				matrix[i][j] = matrix[row - i][col - j]
				matrix[row - i][col - j] = temp
				j += 1
			end
			i += 1
		end
	end
	def show_data(matrix) 
		i = 0
		while (i < self.rows) 
			j = 0
			while (j < self.cols) 
				print(" ", matrix[i][j])
				j += 1
			end
			print("\n")
			i += 1
		end
		print("\n")
	end
end
def main() 
	matrix = [
		[1, 2, 3, 4, 5],
		[6, 7, 8, 9, 10],
		[11, 12, 13, 14, 15],
		[16, 17, 18, 19, 20],
		[21, 22, 23, 24, 25]
	]
	obj = MyMatrix.new(matrix)
	obj.show_data(matrix)
	obj.rotate_matrix(matrix)
	obj.show_data(matrix)
end

main()

Output

 1 2 3 4 5
 6 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25

 25 24 23 22 21
 20 19 18 17 16
 15 14 13 12 11
 10 9 8 7 6
 5 4 3 2 1

/*
  Scala Program
  Rotate a Matrix by 180 degree
*/
class MyMatrix(var rows: Int,var cols: Int) {


	def this(matrix: Array[Array[Int]]) {
		//Get matrix size
		this(matrix.length,matrix(0).length);
	}
	def rotate_matrix(matrix: Array[Array[Int]]): Unit = {
		var temp: Int = 0;
		val row: Int = this.rows - 1;
		val col: Int = this.cols - 1;
		var row_size: Int = ((this.rows / 2).toInt);
		var col_size: Int = this.cols;

		if (this.rows % 2 == 1) {
			//When in case number of rows are Odd size
			row_size = ((this.rows / 2).toInt) + 1;
		}
		var i: Int = 0;
		while (i < row_size) {
			if ((this.rows / 2).toInt == i) {
				//When in case number of rows are Odd size
				//In This case reverse middle row element
				col_size = (this.cols / 2).toInt;
			} else {
				col_size = this.cols;
			}
			var j: Int = 0;
			while (j < col_size) {
				//Swap element
				temp = matrix(i)(j);
				matrix(i)(j) = matrix(row - i)(col - j);
				matrix(row - i)(col - j) = temp;
				j += 1;
			}
			i += 1;
		}
	}
	def show_data(matrix: Array[Array[Int]]): Unit = {
		var i: Int = 0;
		while (i < this.rows) {
			var j: Int = 0;
			while (j < this.cols) {
				print(" " + matrix(i)(j));
				j += 1;
			}
			print("\n");
			i += 1;
		}
		print("\n");
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var matrix: Array[Array[Int]] = Array(
			Array(1, 2, 3, 4, 5),
			Array(6, 7, 8, 9, 10),
			Array(11, 12, 13, 14, 15),
			Array(16, 17, 18, 19, 20),
			Array(21, 22, 23, 24, 25));
		val obj: MyMatrix = new MyMatrix(matrix);
		obj.show_data(matrix);
		obj.rotate_matrix(matrix);
		obj.show_data(matrix);
	}
}

Output

 1 2 3 4 5
 6 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25

 25 24 23 22 21
 20 19 18 17 16
 15 14 13 12 11
 10 9 8 7 6
 5 4 3 2 1
/*
  Swift Program
  Rotate a Matrix by 180 degree
*/
class MyMatrix {
	var rows: Int;
	var cols: Int;
	init(_ matrix: [
		[Int]
	]) {
		//Get matrix size
		self.rows = matrix.count;
		self.cols = matrix[0].count;
	}
	func rotate_matrix(_ matrix: inout [
		[Int]
	]) {
		var temp: Int = 0;
		let row: Int = self.rows - 1;
		let col: Int = self.cols - 1;
		var row_size: Int = (self.rows / 2);
		var col_size: Int = self.cols;
		if (self.rows % 2 == 1) {
			//When in case number of rows are Odd size
			row_size = (self.rows / 2) + 1;
		}
		var i: Int = 0;
		while (i < row_size) {
			if (self.rows / 2 == i) {
				//When in case number of rows are Odd size
				//In This case reverse middle row element
				col_size = self.cols / 2;
			} else {
				col_size = self.cols;
			}
			var j: Int = 0;
			while (j < col_size) {
				//Swap element
				temp = matrix[i][j];
				matrix[i][j] = matrix[row - i][col - j];
				matrix[row - i][col - j] = temp;
				j += 1;
			}
			i += 1;
		}
	}
	func show_data(_ matrix: [
		[Int]
	]) {
		var i: Int = 0;
		while (i < self.rows) {
			var j: Int = 0;
			while (j < self.cols) {
				print(" ", matrix[i][j], terminator: "");
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
	}
}
func main() {
	var matrix: [
		[Int]
	] = [
		[1, 2, 3, 4, 5],
		[6, 7, 8, 9, 10],
		[11, 12, 13, 14, 15],
		[16, 17, 18, 19, 20],
		[21, 22, 23, 24, 25]
	];
	let obj: MyMatrix = MyMatrix(matrix);
	obj.show_data(matrix);
	obj.rotate_matrix(&matrix);
	obj.show_data(matrix);
}
main();

Output

  1  2  3  4  5
  6  7  8  9  10
  11  12  13  14  15
  16  17  18  19  20
  21  22  23  24  25

  25  24  23  22  21
  20  19  18  17  16
  15  14  13  12  11
  10  9  8  7  6
  5  4  3  2  1




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment