# Find Multiplication of Diagonal Elements of a Matrix in Java

In Java, Array is an object. It is a non-primitive data type which stores values of similar data type. The matrix in java is nothing but a multi-dimensional array which represents multiple rows and columns.

Here we have given a matrix which contains set of elements and as per the problem statement we have to find the multiplication of diagonal elements.

In this article we will see, how it can be done by using Java programming language.

## To show you some instances

### Instance-1

Given matrix =

21 	22	23
24	25	26
27	28	29

• Main diagonal elements are: 21, 25 and 29

• Anti-diagonal elements are: 23, 25 and 27

• Multiplication result of both the diagonals elements are: 15225 and 15525

### Instance-2

Given matrix =

9 	2	2	4
1	7	2	6
2	2	4	3
1	4	7	8

• Main diagonal elements are: 9, 7, 4 and 8

• Anti-diagonal elements are: 4, 2, 2 and 1

• Multiplication result of both the diagonals elements are: 2016 and 16.

### Instance-3

Given matrix =

1 	2	3
4	5	6
7	8	9

• Main diagonal elements are: 1, 5 and 9

• Anti-diagonal elements are: 3, 5 and 7

• Multiplication result of both the diagonals elements are: 45 and 105

### Algorithm

• Step-1 − Declare and initialize a 3x3 square matrix called input matrix.

• Step-2 − Declare and initialize two variables, mainDiagonalProd and antiDiagonalProd, to store the product of the values on the main and anti-diagonal of the matrix, respectively.

• Step-3 − Calculates the product of the values on the main diagonal by iterating through the matrix and multiplying the values at each index where the row and column are equal.

• Step-4 − Then calculates the product of the values on the anti-diagonal by iterating through the matrix and multiplying the values at each index where the sum of the row and column is equal to the length of the matrix minus one.

• Step-5 − Finally, prints the calculated product of both the main diagonal and anti-diagonal.

### Syntax

The length method in Java returns the length of the given matrix.

Below refers to the syntax of it −

inputMatrix.length


where, ‘inputMatrix’ refers to the given matrix.

## 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 Elements

In this approach, matrix elements will be initialized in the program. Then as per the algorithm calculate the multiplication of diagonal elements of that matrix.

Here we have found out multiplication value of both the diagonal elements separately.

### Example

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

//declare and initiate a square matrix
int[][] inputMatrix = {{2, 1, 3}, {4, 9, 6}, {1, 8, 4}};

//declare and initiate two integer variable to store both the product values
int mainDiagonalProd = 1;
int antiDiagonalProd = 1;

//find out main diagonal product
for (int i = 0; i < inputMatrix.length; i++) {
mainDiagonalProd *= inputMatrix[i][i];
}

//find out anti-diagonal product
for (int i = 0; i < inputMatrix.length; i++) {
antiDiagonalProd *= inputMatrix[i][inputMatrix.length - i - 1];
}

// print the result
System.out.println("Main Diagonal Product: " + mainDiagonalProd);
System.out.println("Anti-Diagonal Product: " + antiDiagonalProd);
}
}


### Output

Main Diagonal Product: 72
Anti-Diagonal Product: 27


## 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 calculate the multiplication of diagonal elements of that matrix.

Here we have found out multiplication value of both the diagonal elements together.

### Example

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

//declare and initiate a square matrix
int[][] inputMatrix = {{12, 13, 14, 3}, {2, 23, 11, 1}, {53, 23, 23, 56}, {10, 1, 4, 5}};

// Call the user-defined method
int result = prodDiogonalElements(inputMatrix);

// Print the result
System.out.println("The multiplication of diagonal elements is: " + result);

}

// user-defined method to find out product value of diagonal elements
public static int prodDiogonalElements(int[][] mat){

// declare and initiate a integer variable to store the product value
int result = 1;

// initiate the loop and multiply the elements of diagonal
for (int a = 0; a < mat.length; a++){
result *= mat[a][a];
}
return result;
}
}


### Output

The multiplication of diagonal elements is: 31740


In this article, we explored different approaches to find multiplication of diagonal elements of a matrix using Java programming language.

Updated on: 04-May-2023

313 Views 