# Sum of middle row and column in Matrix

Given an integer matrix of odd dimensions (3 * 3, 5 * 5). then the task is to find the sum of the middle row & column elements.

**Examples: **

Input :2 5 7 3 7 2 5 6 9Output :Sum of middle row = 12 Sum of middle column = 18Input :1 3 5 6 7 3 5 3 2 1 1 2 3 4 5 7 9 2 1 6 9 1 5 3 2Output :Sum of middle row = 15 Sum of middle column = 18

**Implementation:**

## CPP

`// C++ program to find sum of ` `// middle row and column in matrix` `#include <iostream>` `using` `namespace` `std;` `const` `int` `MAX = 100;` `void` `middlesum(` `int` `mat[][MAX], ` `int` `n)` `{` ` ` `int` `row_sum = 0, col_sum = 0;` ` ` ` ` `//loop for sum of row` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `row_sum += mat[n / 2][i];` ` ` ` ` `cout << ` `"Sum of middle row = "` ` ` `<< row_sum<<endl;` ` ` ` ` `//loop for sum of column` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `col_sum += mat[i][n / 2];` ` ` ` ` `cout << ` `"Sum of middle column = "` ` ` `<< col_sum;` `}` `// Driver function` `int` `main()` `{` ` ` `int` `mat[][MAX] = {{2, 5, 7},` ` ` `{3, 7, 2}, ` ` ` `{5, 6, 9}};` ` ` ` ` `middlesum(mat, 3);` ` ` ` ` `return` `0;` `}` |

## Java

`// java program to find sum of ` `// middle row and column in matrix` `import` `java.io.*;` `class` `GFG {` ` ` `static` `int` `MAX = ` `100` `;` ` ` `static` `void` `middlesum(` `int` `mat[][], ` `int` `n)` `{` ` ` `int` `row_sum = ` `0` `, col_sum = ` `0` `;` ` ` ` ` `// loop for sum of row` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++)` ` ` `row_sum += mat[n / ` `2` `][i];` ` ` ` ` `System.out.println ( ` `"Sum of middle row = "` ` ` `+ row_sum);` ` ` ` ` `// loop for sum of column` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++)` ` ` `col_sum += mat[i][n / ` `2` `];` ` ` ` ` `System.out.println ( ` `"Sum of middle column = "` ` ` `+ col_sum);` `}` `// Driver function` ` ` `public` `static` `void` `main (String[] args) {` ` ` `int` `mat[][] = {{` `2` `, ` `5` `, ` `7` `},` ` ` `{` `3` `, ` `7` `, ` `2` `}, ` ` ` `{` `5` `, ` `6` `, ` `9` `}};` ` ` ` ` `middlesum(mat, ` `3` `);` ` ` ` ` `}` `}` `// This code is contributed by vt_m.` |

## Python3

`# Python program to find sum of ` `# middle row and column in matrix` ` ` `def` `middlesum(mat,n):` ` ` ` ` `row_sum ` `=` `0` ` ` `col_sum ` `=` `0` ` ` ` ` `# loop for sum of row` ` ` `for` `i ` `in` `range` `(n):` ` ` `row_sum ` `+` `=` `mat[n ` `/` `/` `2` `][i]` ` ` ` ` `print` `(` `"Sum of middle row = "` `,` ` ` `row_sum)` ` ` ` ` `# loop for sum of column` ` ` `for` `i ` `in` `range` `(n):` ` ` `col_sum ` `+` `=` `mat[i][n ` `/` `/` `2` `]` ` ` ` ` `print` `(` `"Sum of middle column = "` `,` ` ` `col_sum)` `# Driver code` `mat` `=` `[[` `2` `, ` `5` `, ` `7` `],` ` ` `[` `3` `, ` `7` `, ` `2` `], ` ` ` `[` `5` `, ` `6` `, ` `9` `]]` ` ` `middlesum(mat, ` `3` `)` `# This code is contributed` `# by Anant Agarwal.` |

## C#

`// C# program to find sum of ` `// middle row and column in matrix` `using` `System;` `class` `GFG {` ` ` ` ` `//static int MAX = 100;` ` ` ` ` `static` `void` `middlesum(` `int` `[,]mat, ` `int` `n)` ` ` `{` ` ` ` ` `int` `row_sum = 0, col_sum = 0;` ` ` ` ` `// loop for sum of row` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `row_sum += mat[n / 2, i];` ` ` ` ` `Console.WriteLine ( ` `"Sum of middle row = "` ` ` `+ row_sum);` ` ` ` ` `// loop for sum of column` ` ` `for` `(` `int` `i = 0; i < n; i++)` ` ` `col_sum += mat[i, n / 2];` ` ` ` ` `Console.WriteLine ( ` `"Sum of middle column = "` ` ` `+ col_sum);` ` ` `}` ` ` `// Driver function` ` ` `public` `static` `void` `Main () {` ` ` `int` `[,]mat = {{2, 5, 7},` ` ` `{3, 7, 2}, ` ` ` `{5, 6, 9}};` ` ` ` ` `middlesum(mat, 3);` ` ` ` ` `}` `}` `// This code is contributed by vt_m.` |

## PHP

`<?php` `// PHP program to find sum of ` `// middle row and column in matrix` `function` `middlesum( ` `$mat` `, ` `$n` `)` `{` ` ` `$row_sum` `= 0; ` `$col_sum` `= 0;` ` ` ` ` `//loop for sum of row` ` ` `for` `( ` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++)` ` ` `$row_sum` `+= ` `$mat` `[` `$n` `/ 2][` `$i` `];` ` ` ` ` `echo` `"Sum of middle row = "` ` ` `, ` `$row_sum` `,` `"\n"` `;` ` ` ` ` `//loop for sum of column` ` ` `for` `( ` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++)` ` ` `$col_sum` `+= ` `$mat` `[` `$i` `][` `$n` `/ 2];` ` ` ` ` `echo` `"Sum of middle column = "` ` ` `, ` `$col_sum` `;` `}` `// Driver function` ` ` `$mat` `= ` `array` `(` `array` `(2, 5, 7),` ` ` `array` `(3, 7, 2), ` ` ` `array` `(5, 6, 9));` ` ` ` ` `middlesum(` `$mat` `, 3);` ` ` `// This code is contributed by anuj_67.` `?>` |

## Javascript

`<script>` `// Javascript program to find sum of ` `// middle row and column in matrix ` `var` `MAX = 100;` ` ` `function` `middlesum(mat , n)` ` ` `{` ` ` `var` `row_sum = 0, col_sum = 0;` ` ` `// loop for sum of row` ` ` `for` `(i = 0; i < n; i++)` ` ` `row_sum += mat[parseInt(n / 2)][i];` ` ` `document.write(` ` ` `"Sum of middle row = "` `+ row_sum+` `"<br/>"` ` ` `);` ` ` `// loop for sum of column` ` ` `for` `(i = 0; i < n; i++)` ` ` `col_sum += mat[i][parseInt(n / 2)];` ` ` `document.write(` ` ` `"Sum of middle column = "` `+ col_sum` ` ` `);` ` ` `}` ` ` `// Driver function` ` ` ` ` `var` `mat = [ [ 2, 5, 7 ], ` ` ` `[ 3, 7, 2 ],` ` ` `[ 5, 6, 9 ] ];` ` ` `middlesum(mat, 3);` `// This code contributed by aashish1995` `</script>` |

**Output**

Sum of middle row = 12 Sum of middle column = 18

**Time Complexity : O(n) ****Auxiliary Space : O(1) **using constant space to initialize row_sum and col_sum variables