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

In the previous article, we have seen Java Program to Find the Sum of Middle Row and Middle Column of a Matrix

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

Java Program to Find the Product 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 Product of Middle row and Column.

Method-1: Java Program to Find the Product of Middle Row and Middle Column of a Matrix

Approach:

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

Program:

import java.util.*;
public class Main 
{
   public static void main(String args[])
   {
       // Initializing the 3X3 matrix i.e. 2D array 
        int mat[][]={{1,2,3},{4,5,6},{7,8,9}};
        int row, col, Pro_row=1,Pro_col=1 ;

        for (int i = 0; i < 3; i++) 
                 Pro_row *= mat[3 / 2][i];
        for (int i = 0; i < 3; i++) 
                 Pro_col *= mat[i][3 / 2];
        int res= Pro_row*Pro_col;
        System.out.println("Product of middle row is : " + Pro_row);
        System.out.println("Product of middle Column is : " + Pro_col);
        System.out.println("Product of middle Row and Column is : " + res);
   }
}

Output:

Product of middle row is : 120
Product of middle Column is : 80
Product of middle Row and Column is : 9600

Method-2:Java Program to Find the Product of Middle Row and Middle Column of a Matrix

Approach :

  • Take input of a matrix.
  • Take a for loop to calculate product of middle row value . for each iteration calculate Pro_row += mat[3 / 2][i]
  • Take a for loop to calculate product of middle column value . for each iteration calculate Pro_col += mat[i][3 / 2]
  • Print the 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, Pro_row=1,Pro_col=1 ;
        // Taking matrix input
        System.out.println("Enter 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++) 
                 Pro_row *= mat[3 / 2][i];
        for (int i = 0; i < 3; i++) 
                 Pro_col *= mat[i][3 / 2];
        int res= Pro_row*Pro_col;
        System.out.println("Product of middle row is : " + Pro_row);
        System.out.println("Product of middle Column is : " + Pro_col);
        System.out.println("Product 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 : 120
Sum of middle Column is : 80
Sum of middle Row and Column is : 9600

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: