# Java Program to Find the Sum of Middle Row and Middle Column of a Matrix

In the previous article, we have seen Java Program to Print the elements of the Matrix in Spiral Form

In this article we are going to see how we can write a program how to calculate Sum of Middle row and column.

## Java Program to Find the Sum of Middle Row and Middle Column of a Matrix

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
| A20   A21   A22 | 3*3
• Matrix A represents a 3*3 matrix.
• A‘ represents the matrix element.
• Aij‘ represents the matrix element at it’s matrix position/index.
• i‘ represents the row index.
• j‘ represents the column index
• Means A00=Aij  where i=0 and j=0,  A01=aij where i=0 and j=1 and like this.
• Here we have started row value from 0 and column value from 0.

Let’s see different ways to calculate Sum of Middle row and Column.

### Method-1: Java Program to Find the Sum of Middle Row and Middle Column of a Matrix By Static Initialization of Array Elements

Approach:

• Initialize and declare a matrix.
• Take a for loop to calculate sum of middle row value . for each iteration calculate Sum_row += mat[3 / 2][i]
• Take a for loop to calculate sum of middle column value . for each iteration calculate Sum_col += mat[i][3 / 2]
• Print 2 results .

Program:

import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);

// Initializing the 3X3 matrix i.e. 2D array
int mat[][]={{10,20,30},{40,50,60},{70,80,90}};
int Sum_row=0,Sum_col=0 ;
for (int i = 0; i < 3; i++)
Sum_row += mat[3 / 2][i];
for (int i = 0; i < 3; i++)
Sum_col += mat[i][3 / 2];
System.out.println("Sum of middle row is : " + Sum_row);
System.out.println("Sum of middle Column is : " + Sum_col);
int res = Sum_row+Sum_col;
System.out.println("Sum of middle row and Column is : " + res);
}
}

Output:

Sum of middle row is : 150
Sum of middle Column is : 150
Sum of middle row and Column is : 300

### Method-2: Java Program to Find the Sum of Middle Row and Middle Column of a Matrix By Dynamic Initialization of Array Elements

Approach :

• Take input of a matrix.
• Take a for loop to calculate sum of middle row value . for each iteration calculate Sum_row += mat[3 / 2][i]
• Take a for loop to calculate sum of middle column value . for each iteration calculate Sum_col += mat[i][3 / 2]
• Print 2 results .

Program :

import java.util.*;
public class Main
{
public static void main(String args[])
{
Scanner s = new Scanner(System.in);
// Initializing the 3X3 matrix i.e. 2D array
int mat[][] = new int[3][3];
int row, col, Sum_row=0,Sum_col=0 ;
// Taking matrix input
System.out.println("\nEnter matrix elements");
for(row=0;row<3;row++)
for(col=0;col<3;col++)
mat[row][col] = s.nextInt();
for (int i = 0; i < 3; i++)
Sum_row += mat[3 / 2][i];
for (int i = 0; i < 3; i++)
Sum_col += mat[i][3 / 2];
int res= Sum_row+Sum_col;
System.out.println("Sum of middle row is : " + Sum_row);
System.out.println("Sum of middle Column is : " + Sum_col);
System.out.println("Sum of middle Row and Column is : " + res);
}
}


Output:

Enter matrix elements 1 2 3 4 5 6 7 8 9
Sum of middle row is : 15
Sum of middle Column is : 15
Sum of middle Row and Column is : 30

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Related Java Programs: