# C Program to Display Middle Row and Column of Matrix

## Introduction

In this post, I am going to write a c program to display the middle row and column of the matrix. For this, we have to take N * N matrix where the value of N is an odd number because we have to print middle row and columns of a matrix. Just see the expected output. It helps us to understand the logic of the program.

Also Read: Arrays in C for Complete Beginners

### Expected Output

``````Enter odd value of N for N * N matrix
5
Enter 5 * 5 matrix
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Original 5 * 5 Matrix is
1  1  1  1  1
1  1  1  1  1
1  1  1  1  1
1  1  1  1  1
1  1  1  1  1

Middle Row and Column of Matrix is
1
1
1 1 1 1 1
1
1``````

As you can see the above expected output, we have performed the following steps.

1. Ask the user to enter the size of the matrix.
2. Now, we have asked the user to enter the array elements according to the size of an array.
3. We have displayed the original array.
4. And, at the last, we have displayed the middle row and column of the above matrix.

### Logic of the C Program to Display middle row and column

Here, we are reading the odd matrix like 3 * 3, 5 * 5 and so on. After that, we will divide this number i.e. 3 or 5 or N by 2. Both are integer numbers so we will get integer result. For example, if the value of N is 5 then, after dividing this 5 by 2, we will get answer 2.

In the loop, we will check row or column must be equal to the value 2. If this is true, then only we will print the matrix element. See the following c program to display middle row and column of the matrix.

### C Program to Display Middle Row and Column of Matrix

``````#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,i,j,n,mid;
printf("Enter odd value of N for N * N matrix\n");
scanf("%d",&n);
if(n%2==0)
{
printf("Sorry, you have entered even value.");
return 0;
}
printf("Enter %d * %d matrix\n",n,n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Original %d * %d Matrix is\n",n,n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf(" %d ",a[i][j]);
}
printf("\n");
}
mid=n/2;
printf("\nMiddle Row and Column of Matrix is\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((i==mid)||(j==mid))
{
printf(" %d",a[i][j]);
}
else
{
printf("  ");
}
}
printf("\n");
}
return 0;
}
``````

### Explanation

In the above c program, the important code is given below which is displaying the middle row and column.

Also Read: Programs on Arrays in C

``````    mid=n/2;
printf("\nMiddle Row and Column of Matrix is\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if((i==mid)||(j==mid))
{
printf(" %d",a[i][j]);
}
else
{
printf("  ");
}
}
printf("\n");
}``````

#### mid=n/2

If the value of n is 5, then the value of mid becomes 2.

#### if( ( i==mid ) || ( j == mid ) )

Here, the i represents the row number and j represent column number. In the above if statement, we are comparing if the value of i or the value of j is equal to the value of mid, only then we will display the matrix element. Otherwise, print blank space.

This is the important code in the above program. Remaining code is just reading and storing the matrix elements.

I hope you have understood this program. If you have any doubt or feedback, then feel free to contact me. Thank you.