# Find Product of Sum of First & last Row Elements of a Matrix in Java

Matrices are nothing but it’s more than a collection of data elements arranged in a rectangular layout that is two-dimensional. In Java, an array with two dimensions can be considered as a matrix.

As per the problem statement the task is to find the product of sum of all elements in first row and last row.

Let’s deep dive into this article, to know how it can be done by using Java programming language.

## To show you some instances

### Instance-1

Suppose the original matrix is

{{15,5,9},
{4,14,16},
{7,8,19}};

• After finding product of sum of all elements in first row and last row in a matrix, the result index will be −

• Product of sum of all elements in first row and last row is: 986

### Instance-2

Suppose the original matrix is

{{11,7,2},
{3,4,24},
{12,8,15}};

• After finding product of sum of all elements in first row and last row in a matrix, the result index will be −

• Product of sum of all elements in first row and last row is: 700

## Algorithm

• Step-1 − Initialize and declare the matrix.

• Step-2 − Initialize and declare the first row and last row as 0.

• Step-3 − Calculate the sum of first row elements and last row elements using for loop.

• Step-4 − Find sum of all elements of the first and last row.

• Step-5 − Find product between them.

• Step-6 − Print the result.

## Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Initialization of matrix Elements

• By Using User Defined Method

Let’s see the program along with its output one by one.

## Approach-1: By Using Static Initialization of Matrix

In this approach, matrix elements will be initialized in the program. Then as per the algorithm find product of sum of all elements in first row and last row.

### Example

public class Main{

//main method
public static void main(String args[]){

//Initialising and declaring matrix
int arr[][] = {{15,5,9},
{4,14,16},
{7,8,19}};
int row, col ;

//Initialising and declaring the first row and last row as 0
int first_Row_Sum=0;
int last_Row_Sum=0;

//for loop to calculate the sum of first row elements and last row elements
for(row=0;row<3;row++){
for(col=0;col<3;col++){

//finding sum of all elements of the first row
if(row==0)
first_Row_Sum = first_Row_Sum+arr[0][col];
//finding sum of all elements of the last row
else if(row==2)
last_Row_Sum = last_Row_Sum+arr[2][col];
}
}

//finding product between sum of first row elements and last row elements
int prod = first_Row_Sum * last_Row_Sum;

//Printing the product of first row elements and last row elements
System.out.print("Product of sum of all elements in first row and last row is: "+prod);
}
}


### Output

Product of sum of all elements in first row and last row is: 986


## Approach-2: By Using User Defined Method

In this approach, matrix elements will be initialized in the program. Then call a user defined method by passing the matrix as parameter and inside method as per the algorithm find product of sum of all elements in first row and last row.

### Example

public class Main{
//main method
public static void main(String args[]){

//Initialising and declaring matrix
int arr[][] = {{11,7,2},
{3,4,24},
{12,8,15}};
func(arr);
}

//user defined method
static void func(int arr[][]){
int row, col ;

//Initialising and declaring the first row and last row as 0
int first_Row_Sum=0;
int last_Row_Sum=0;

//for loop to calculate the sum of first row elements and last row elements
for(row=0;row<3;row++){
for(col=0;col<3;col++){

//finding sum of all elements of the first row
if(row==0)
first_Row_Sum = first_Row_Sum+arr[0][col];
//finding sum of all elements of the last row
else if(row==2)
last_Row_Sum = last_Row_Sum+arr[2][col];
}
}

//finding product between sum of first row elements and last row elements
int prod = first_Row_Sum * last_Row_Sum;

//Printing the product of first row elements and last row elements
System.out.print("Product of sum of all elements in first row and last row is: "+prod);
}
}


### Output

Product of sum of all elements in first row and last row is: 700


In this article, we explored different approaches to check diagonally dominant matrix by using Java programming language.

Updated on: 04-May-2023

132 Views