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

**Examples:**

Input:mat[][] = {{2, 1, 7}, {3, 7, 2}, {5, 4, 9}}Output:Product of middle row = 42 Product of middle column = 28 Explanation : Product of Middle row elements (3*7*2) Product of Middle Column elements (1*7*4)Input:mat[][] = { {1, 3, 5, 6, 7}, {3, 5, 3, 2, 1}, {1, 2, 3, 4, 5}, {7, 9, 2, 1, 6}, {9, 1, 5, 3, 2}}Output:Product of middle row = 120 Product of middle column = 450

**Approach:** As the given matrix is of odd dimensions so the middle row and column will be at n/2 th index always. So, Run a loop from i = 0 to N and product all the elements of middle row i.e. **row_prod *= mat[n / 2][i]**. Similarly, product of elements of middle column will be **col_prod *= mat[i][n / 2]**.

Below is the implementation of the above approach:

## C++

`// C++ program to find product of ` `// middle row and middle column in matrix ` `#include <iostream> ` `using` `namespace` `std; ` `const` `int` `MAX = 100; ` ` ` `void` `middleProduct(` `int` `mat[][MAX], ` `int` `n) ` `{ ` ` ` ` ` `// loop for product of row and column ` ` ` `int` `row_prod = 1, col_prod = 1; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `row_prod *= mat[n / 2][i]; ` ` ` `col_prod *= mat[i][n / 2]; ` ` ` `} ` ` ` ` ` `// Print result ` ` ` `cout << ` `"Product of middle row = "` ` ` `<< row_prod << endl; ` ` ` ` ` `cout << ` `"Product of middle column = "` ` ` `<< col_prod; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `mat[][MAX] = { { 2, 1, 7 }, ` ` ` `{ 3, 7, 2 }, ` ` ` `{ 5, 4, 9 } }; ` ` ` ` ` `middleProduct(mat, 3); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find product of ` `// middle row and middle column in matrix ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `MAX = ` `100` `; ` ` ` `static` `void` `middleProduct(` `int` `mat[][], ` `int` `n) ` `{ ` ` ` ` ` `// loop for product of row and column ` ` ` `int` `row_prod = ` `1` `, col_prod = ` `1` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `row_prod *= mat[n / ` `2` `][i]; ` ` ` `col_prod *= mat[i][n / ` `2` `]; ` ` ` `} ` ` ` ` ` `// Print result ` ` ` `System.out.print(` `"Product of middle row = "` ` ` `+ row_prod); ` ` ` ` ` `System.out.print( ` `"Product of middle column = "` ` ` `+ col_prod); ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `mat[][] = { { ` `2` `, ` `1` `, ` `7` `}, ` ` ` `{ ` `3` `, ` `7` `, ` `2` `}, ` ` ` `{ ` `5` `, ` `4` `, ` `9` `} }; ` ` ` ` ` `middleProduct(mat, ` `3` `); ` ` ` `} ` `} ` `// This code is contributed by shs ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find product of ` `# middle row and middle column in matrix ` ` ` `MAX` `=` `100` ` ` `def` `middleProduct(mat, n): ` ` ` ` ` `# loop for product of row and column ` ` ` `row_prod ` `=` `1` ` ` `col_prod ` `=` `1` ` ` `for` `i ` `in` `range` `(n) : ` ` ` `row_prod ` `*` `=` `mat[n ` `/` `/` `2` `][i] ` ` ` `col_prod ` `*` `=` `mat[i][n ` `/` `/` `2` `] ` ` ` ` ` `# Print result ` ` ` `print` `(` `"Product of middle row = "` `, ` ` ` `row_prod) ` ` ` ` ` `print` `(` `"Product of middle column = "` `, ` ` ` `col_prod) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `mat ` `=` `[[ ` `2` `, ` `1` `, ` `7` `], ` ` ` `[ ` `3` `, ` `7` `, ` `2` `], ` ` ` `[ ` `5` `, ` `4` `, ` `9` `]] ` ` ` ` ` `middleProduct(mat, ` `3` `) ` ` ` `# This code is contributed by ita_c ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find product of ` `// middle row and middle column in matrix ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `//static int MAX = 100; ` ` ` `static` `void` `middleProduct(` `int` `[,]mat, ` `int` `n) ` `{ ` ` ` ` ` `// loop for product of row and column ` ` ` `int` `row_prod = 1, col_prod = 1; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `row_prod *= mat[n / 2,i]; ` ` ` `col_prod *= mat[i,n / 2]; ` ` ` `} ` ` ` ` ` `// Print result ` ` ` `Console.WriteLine(` `"Product of middle row = "` ` ` `+ row_prod); ` ` ` ` ` `Console.WriteLine( ` `"Product of middle column = "` ` ` `+ col_prod); ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main () { ` ` ` `int` `[,]mat = { { 2, 1, 7 }, ` ` ` `{ 3, 7, 2 }, ` ` ` `{ 5, 4, 9 } }; ` ` ` ` ` `middleProduct(mat, 3); ` ` ` `} ` `} ` `// This code is contributed by shs ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find product of ` `// middle row and middle column in matrix ` ` ` `$MAX` `= 100; ` ` ` `function` `middleProduct(` `$mat` `, ` `$n` `) ` `{ ` ` ` ` ` `// loop for product of row and column ` ` ` `$row_prod` `= 1; ` `$col_prod` `= 1; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `$row_prod` `*= ` `$mat` `[` `$n` `/ 2][` `$i` `]; ` ` ` `$col_prod` `*= ` `$mat` `[` `$i` `][` `$n` `/ 2]; ` ` ` `} ` ` ` ` ` `// Print result ` ` ` `echo` `"Product of middle row = "` `. ` ` ` `$row_prod` `. ` `"\n"` `; ` ` ` ` ` `echo` `"Product of middle column = "` `. ` ` ` `$col_prod` `; ` `} ` ` ` `// Driver code ` `$mat` `= ` `array` `(` `array` `( 2, 1, 7 ), ` ` ` `array` `( 3, 7, 2 ), ` ` ` `array` `( 5, 4, 9 )); ` ` ` `middleProduct(` `$mat` `, 3); ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Product of middle row = 42 Product of middle column = 28

**Time Complexity:** O(n)

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:

- Sum of middle row and column in Matrix
- Find a Square Matrix such that sum of elements in every row and column is K
- Count of ways to generate a Matrix with product of each row and column as 1 or -1
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Check if a given matrix can be converted to another given matrix by row and column exchanges
- Search in a row wise and column wise sorted matrix
- Print all elements in sorted order from row and column wise sorted matrix
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
- Count zeros in a row wise and column wise sorted matrix
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Sum of matrix element where each elements is integer division of row and column
- Check if sums of i-th row and i-th column are same in matrix
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Minimum operations required to make each row and column of matrix equals
- Sort the matrix row-wise and column-wise
- Program to find the Sum of each Row and each Column of a Matrix
- Minimum element of each row and each column in a matrix
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix

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.