You are given a matrix of order **N*N**. The task is to find the resultant matrix by adding the mirror image of given matrix with the matrix itself.

**Examples**:

Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}Output: 4 4 4 10 10 10 16 16 16Explanation: Resultant Matrix = {{1, 2, 3}, {{3, 2, 1}, {4, 5, 6}, + {6, 5, 4}, {7, 8, 9}} {9, 8, 7}}Input: mat[][] = {{1, 2}, {3, 4}}Output: 3 3 7 7

While finding the mirror image of matrix the row of each element will remain same but the value of its columns will reshuffle. For any element A_{ij} its new position in mirror image will be A_{i(n-j)}. After getting the mirror image of matrix add it to original matrix and print the result.

**Points to take care:**

- Indexing of matrix will start from 0, 0 and ends on n-1, n-1 hence position of any element A
_{ij}will be A_{i(n-1-j).} - While printing the result take care of proper output format

Below is the implementation of the above approach:

## C++

`// C++ program to find sum of matrix and ` `// its mirror image ` `#include <bits/stdc++.h> ` ` ` `#define N 4 ` `using` `namespace` `std; ` ` ` `// Function to print the resultant matrix ` `void` `printSum(` `int` `mat[][N]) ` `{ ` ` ` `for` `(` `int` `i = 0; i < N; i++) { ` ` ` `for` `(` `int` `j = 0; j < N; j++) { ` ` ` `cout << setw(3) << mat[i][N - 1 - j] + mat[i][j] << ` `" "` `; ` ` ` `} ` ` ` ` ` `cout << ` `"\n"` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `mat[N][N] = { { 2, 4, 6, 8 }, ` ` ` `{ 1, 3, 5, 7 }, ` ` ` `{ 8, 6, 4, 2 }, ` ` ` `{ 7, 5, 3, 1 } }; ` ` ` ` ` `printSum(mat); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find sum of ` `// matrix and its mirror image ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` `static` `int` `N = ` `4` `; ` ` ` `// Function to print the ` `// resultant matrix ` `static` `void` `printSum(` `int` `mat[][]) ` `{ ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = ` `0` `; j < N; j++) ` ` ` `{ ` ` ` `System.out.print((mat[i][N - ` `1` `- j] + ` ` ` `mat[i][j]) + ` `" "` `); ` ` ` `} ` ` ` ` ` `System.out.println(); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `mat[][] = { { ` `2` `, ` `4` `, ` `6` `, ` `8` `}, ` ` ` `{ ` `1` `, ` `3` `, ` `5` `, ` `7` `}, ` ` ` `{ ` `8` `, ` `6` `, ` `4` `, ` `2` `}, ` ` ` `{ ` `7` `, ` `5` `, ` `3` `, ` `1` `} }; ` ` ` ` ` `printSum(mat); ` `} ` `} ` ` ` `// This code is contributed by anuj_67 ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find sum of matrix ` `# and its mirror image ` ` ` `N ` `=` `4` ` ` `# Function to print the resultant matrix ` `def` `printSum(mat): ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `for` `j ` `in` `range` `(N): ` ` ` `print` `(` `'{:>3}'` `.` `format` `(mat[i][N ` `-` `1` `-` `j] ` `+` ` ` `mat[i][j]), end ` `=` `" "` `) ` ` ` ` ` `print` `(` `"\n"` `, end ` `=` `"") ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `mat ` `=` `[[` `2` `, ` `4` `, ` `6` `, ` `8` `], ` ` ` `[` `1` `, ` `3` `, ` `5` `, ` `7` `], ` ` ` `[` `8` `, ` `6` `, ` `4` `, ` `2` `], ` ` ` `[` `7` `, ` `5` `, ` `3` `, ` `1` `]] ` ` ` ` ` `printSum(mat) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum of ` `// matrix and its mirror image ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `N = 4; ` ` ` `// Function to print the ` `// resultant matrix ` `static` `void` `printSum(` `int` `[,]mat) ` `{ ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = 0; j < N; j++) ` ` ` `{ ` ` ` `Console.Write((mat[i, N - 1 - j] + ` ` ` `mat[i, j]) + ` `" "` `); ` ` ` `} ` ` ` ` ` `Console.WriteLine(); ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main () ` `{ ` ` ` `int` `[,]mat = { { 2, 4, 6, 8 }, ` ` ` `{ 1, 3, 5, 7 }, ` ` ` `{ 8, 6, 4, 2 }, ` ` ` `{ 7, 5, 3, 1 } }; ` ` ` ` ` `printSum(mat); ` `} ` `} ` ` ` `// This code is contributed by shs.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find sum of ` `// matrix and its mirror image ` ` ` `// Function to print the ` `// resultant matrix ` `function` `printSum(` `$mat` `) ` `{ ` ` ` `for` `(` `$i` `= 0; ` `$i` `< 4; ` `$i` `++) ` ` ` `{ ` ` ` `for` `(` `$j` `= 0; ` `$j` `< 4; ` `$j` `++) ` ` ` `{ ` ` ` `echo` `(` `$mat` `[` `$i` `][4 - 1 - ` `$j` `] + ` ` ` `$mat` `[` `$i` `][` `$j` `]) . ` `" "` `; ` ` ` `} ` ` ` ` ` `echo` `"\n"` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `$mat` `= ` `array` `(` `array` `(2, 4, 6, 8 ), ` ` ` `array` `(1, 3, 5, 7), ` ` ` `array` `(8, 6, 4, 2), ` ` ` `array` `(7, 5, 3, 1)); ` ` ` `printSum(` `$mat` `); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

10 10 10 10 8 8 8 8 10 10 10 10 8 8 8 8

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Construct Full Binary Tree using its Preorder traversal and Preorder traversal of its mirror tree
- Convert a Binary Tree into its Mirror Tree
- Create a mirror tree from the given binary tree
- Check if the given two matrices are mirror images of one another
- Print all the sub diagonal elements of the given square matrix
- Print all the super diagonal elements of the given square matrix
- Program to print Sum of even and odd elements in an array
- Find column with maximum sum in a Matrix
- Find row with maximum sum in a Matrix
- Sum of alternate elements of a N x N matrix
- Construct BST from its given level order traversal
- Print all the levels with odd and even number of nodes in it | Set-2
- Print all the peaks and troughs in an Array of Integers
- Element in a matrix starting from which anti-clockwise traversal ends at the last element
- Finding the converging element of the diagonals in a square matrix
- Count of ways to traverse a Matrix according to given conditions
- Traverse a given Matrix using Recursion
- Maximum strength in a Matrix after performing specified operations
- Arrange numbers 1 to N^2 in a Zig-Zag Matrix in ascending order
- Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | Set 2

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.