# Print sum of matrix and its mirror image

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 16
Explanation:
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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 Aij its new position in mirror image will be Ai(n-j). After getting the mirror image of matrix add it to original matrix and print the result.

Points to take care:

1. Indexing of matrix will start from 0, 0 and ends on n-1, n-1 hence position of any element Aij will be Ai(n-1-j).
2. 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 ` ` `  `#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; ` `} `

## 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 `

## 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.. `

## PHP

Output:

```10  10  10  10
8   8   8   8
10  10  10  10
8   8   8   8
```

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.