
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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 means the number of row and column must me odd, i.e, not divisible by 2, the task is to find the product of middle row and middle column of that matrix.
Like in the given figure below −
Constraints
Matrix must be a square matrix.
Column and rows must be of odd length.
Input
mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
Output
Product of middle row = 120 Product of middle column = 80
Explanation
Product of middle row = 4 * 5 * 6 = 120 Product of middle column = 2 * 5 * 8 = 80
Input
mat[][] = {{3, 5, 0}, {1, 2, 7}, {9, 0, 5}}
Output
Product of middle row = 14 Product of middle column = 0
Explanation
Product of middle row = 1 * 2 * 7 = 120 Product of middle column = 5 * 2 * 0 = 0
Approach used below is as follows to solve the problem
Take a matrix mat[][] as an input.
Traverse the matrix as from the middle row and middle column
Calculate the product of the middle row and middle column and return the result.
Algorithm
Start In function int product(int mat[][MAX], int n) Step 1→ Declare and initialize rproduct = 1, cproduct = 1 Step 2→ Loop For i = 0 and i < n and i++ Set rproduct = rproduct * mat[n / 2][i] Set cproduct = cproduct * mat[i][n / 2] Step 3→ Print "Product of middle row: rproduct “ Step 4→ Print "Product of middle column: cproduct” In function int main() Step 1→ Declare and initialize mat[][MAX] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } } Step 2→ Call product(mat, MAX) Stop
Example
#include <stdio.h> #define MAX 3 int product(int mat[][MAX], int n){ int rproduct = 1, cproduct = 1; //We will only check the middle elements and //find their products for (int i = 0; i < n; i++) { rproduct *= mat[n / 2][i]; cproduct *= mat[i][n / 2]; } // Printing the result printf("Product of middle row: %d
", rproduct); printf("Product of middle column: %d
", cproduct); return 0; } // Driver code int main(){ int mat[][MAX] = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; product(mat, MAX); return 0; }
Output
If run the above code it will generate the following output −
Product of middle row: 120 Product of middle column: 80
- Related Articles
- Row-wise vs column-wise traversal of matrix in C++
- How to multiply single row matrix and a square matrix in R?
- Matrix row sum and column sum using C program
- How to create a row sum and a row product column in an R data frame?
- Row-wise common elements in two diagonals of a square matrix in C++
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix in C++
- Define column and row names of a square matrix in a single line code if they are same in R.
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix in C++ Program
- How to find the row product of a matrix in R?
- How to search in a row wise and column wise increased matrix using C#?
- How to find the row sum for each column by row name in an R matrix?
- How to find the row and column position of a value as vector in an R matrix?
- Print an N x M matrix such that each row and column has all the vowels in it in C++
- C++ program to find the Sum of each Row and each Column of a Matrix
- How to create a matrix without column and row indices in R?

Advertisements