Product of middle row and column in an odd square matrix in C

Given a square matrix, mat[row][column] where row and column are equal and are of odd length, the task is to find the product of middle row and middle column of that matrix. The matrix must have odd dimensions so there's always a clear middle row and column.

1 2 3 4 5 6 7 8 9 Middle Row: 4×5×6=120 Middle Column: 2×5×8=80 Yellow: Middle Row | Middle Column Red: Common Element

Syntax

int product(int mat[][MAX], int n);
// Where mat is the square matrix, n is the dimension

Algorithm Steps

  • Take a square matrix of odd dimensions as input
  • Find the middle row index: n/2 (integer division)
  • Find the middle column index: n/2
  • Calculate product of all elements in middle row
  • Calculate product of all elements in middle column

Example

#include <stdio.h>
#define MAX 3

int product(int mat[][MAX], int n) {
    int rproduct = 1, cproduct = 1;
    
    // Calculate products of middle row and middle column
    for (int i = 0; i < n; i++) {
        rproduct *= mat[n / 2][i];     // Middle row elements
        cproduct *= mat[i][n / 2];     // Middle column elements
    }
    
    // Display the results
    printf("Product of middle row: %d<br>", rproduct);
    printf("Product of middle column: %d<br>", cproduct);
    
    return 0;
}

int main() {
    int mat[][MAX] = {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 }
    };
    
    printf("Matrix:<br>");
    for (int i = 0; i < MAX; i++) {
        for (int j = 0; j < MAX; j++) {
            printf("%d ", mat[i][j]);
        }
        printf("<br>");
    }
    printf("<br>");
    
    product(mat, MAX);
    return 0;
}
Matrix:
1 2 3 
4 5 6 
7 8 9 

Product of middle row: 120
Product of middle column: 80

Example with Zero Element

#include <stdio.h>
#define SIZE 3

int main() {
    int mat[][SIZE] = {
        { 3, 5, 0 },
        { 1, 2, 7 },
        { 9, 0, 5 }
    };
    
    int rproduct = 1, cproduct = 1;
    
    printf("Matrix:<br>");
    for (int i = 0; i < SIZE; i++) {
        for (int j = 0; j < SIZE; j++) {
            printf("%d ", mat[i][j]);
        }
        printf("<br>");
    }
    
    // Calculate products
    for (int i = 0; i < SIZE; i++) {
        rproduct *= mat[SIZE / 2][i];  // Middle row: 1, 2, 7
        cproduct *= mat[i][SIZE / 2];  // Middle column: 5, 2, 0
    }
    
    printf("\nProduct of middle row: %d<br>", rproduct);
    printf("Product of middle column: %d<br>", cproduct);
    
    return 0;
}
Matrix:
3 5 0 
1 2 7 
9 0 5 

Product of middle row: 14
Product of middle column: 0

Key Points

  • Matrix dimensions must be odd (3×3, 5×5, 7×7, etc.) for a clear middle
  • Middle index is calculated as n/2 using integer division
  • If any element is 0, the entire product becomes 0
  • Time complexity: O(n), Space complexity: O(1)

Conclusion

This approach efficiently calculates the product of middle row and column elements in an odd square matrix using simple iteration. The algorithm works for any odd-dimensioned square matrix and handles edge cases like zero elements correctly.

Updated on: 2026-03-15T12:57:46+05:30

317 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements