In the previous article, we have discussed Java Program to find Sum of Diagonal Elements of a Matrix
In this article we are going to see how we can write a program to calculate the product of both diagonal elements of a matrix in JAVA language.
Program to find Multiplication of Diagonal Elements 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 Arepresents 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=Aijwherei=0andj=0,A01=aijwherei=0andj=1and like this. - Here we have started
rowvalue from 0 andcolumnvalue from 0.
Let’s see different ways to find Multiplication of Diagonal Elements of a Matrix.
Method-1: Java Program to find Multiplication of Diagonal Elements of a Matrix By Static Initialization of Array Elements
Approach:
- Initialize an array of size 3×3 with values.
- Show the array to the user.
- Use two for loops to iterate the rows and columns, then multiply both the diagonal elements.
- Print the output.
Program:
public class Matrix{
public static void main(String args[])
{
// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = {{1,2,3},{4,5,6},{7,8,9}};
int row, col;
long primaryProd = 1, secondaryProd =1;
// Printing the matrix using our user-defined function
printMatrix(arr);
// Loops to find the product
for(row=0;row<3;row++)
for(col=0;col<3;col++)
{
// Only goes in if the element is a diagonal element
if(row==col)
primaryProd *= arr[row][col];
if(row+col==2)
secondaryProd *= arr[row][col];
}
System.out.println("\nProduct of left diagonal : "+primaryProd);
System.out.println("Product of right diagonal : "+secondaryProd);
System.out.println("The product of both diagonal elements are : "+(primaryProd*secondaryProd));
}
// Function to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
System.out.print("The matrix elements are");
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
}
}
Output: The matrix elements are 1 2 3 4 5 6 7 8 9 Product of left diagonal : 45 Product of right diagonal : 105 The product of both diagonal elements are : 4725
Method-2: Java Program to find Multiplication of Diagonal Elements of a Matrix By Dynamic Initialization of Array Elements
Approach:
- Initialize an array of size 3×3.
- Ask the user for input.
- Use two for loops to iterate the rows and columns to input the array elements.
- Use two for loops to iterate the rows and columns, then multiply both the diagonal elements.
- Print the output.
Program:
import java.util.Scanner;
public class Matrix{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);
// Delclaring the 3X3 matrix i.e. 2D array
int arr[][] = new int[3][3];
int row, col, primaryProd = 1, secondaryProd=1;
// Taking the matrix as input
System.out.println("Enter the matrix elements : ");
for(row=0;row<3;row++)
for(col=0;col<3;col++)
arr[row][col] = scan.nextInt();
System.out.print("Matrix : ");
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
// Loops to find the product
for(row=0;row<3;row++)
for(col=0;col<3;col++)
{
// Only goes in if the element is a diagonal element
if(row==col)
primaryProd *= arr[row][col];
if(row+col==2)
secondaryProd *= arr[row][col];
}
System.out.println("\nThe product of left diagonal elements is : "+primaryProd);
System.out.println("The product of right diagonal elements is : "+secondaryProd);
System.out.println("The product of both diagonal elements is : "+(primaryProd*secondaryProd));
}
}
Output: Enter the matrix elements : 1 2 3 4 5 6 7 8 9 Matrix : 1 2 3 4 5 6 7 8 9 The product of left diagonal elements is : 45 The product of right diagonal elements is : 105 The product of both diagonal elements is : 4725
Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.
Related Java Programs: