In the previous article, we have discussed Java Program to Find the Sums of Primary Diagonal and Secondary Diagonal
In this article we are going to see how we can write a program to find the product of sum of primary diagonal elements and secondary diagonal elements of a matrix in JAVA language.
Java Program to Find the Product Between Sum of Two Diagonals 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
wherei=0
andj=0
,A01=aij
wherei=0
andj=1
and like this. - Here we have started
row
value from 0 andcolumn
value from 0.
Let’s see different ways to find product between sum of Primary Diagonal and Secondary Diagonal of the matrix.
Method-1: Java Program to Find the Product Between Sum of Two Diagonals 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.
- Similarly use two for loops to iterate the rows and columns, then calculate the main diagonal sum.
- Repeat the above step to calculate the counter diagonal sum.
- Multiply both of them and 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); // Initializing the 3X3 matrix i.e. 2D array int arr[][] = {{19,25,32},{40,54,62},{70,20,60}}, mainSum = 0, counterSum = 0; int row, col; System.out.print("The array 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]+" "); } } System.out.print("\n"); // Loop to take the sum of main diagonal elements for(row=0;row<3;row++) for(col=0;col<3;col++) { if(row==col) mainSum+=arr[row][col]; // if(row+col==2) } System.out.println("Sum of main diagonal : "+mainSum); // Loop to take the sum of counter diagonal elements for(row=0;row<3;row++) for(col=0;col<3;col++) { if(row+col==2) counterSum+=arr[row][col]; } System.out.println("Sum of counter diagonal : "+counterSum); // Printing product betweeen both diagonals sum System.out.print("\nProduct between sum of both diagonal : "+(mainSum*counterSum)); } }
Output: The array elements are : 19 25 32 40 54 62 70 20 60 Sum of main diagonal : 133 Sum of counter diagonal : 156 Product between sum of both diagonal : 20748
Method-2: Java Program to Find the Product Between Sum of Two Diagonals of a Matrix By Dynamic Initialization of Array Elements
Approach:
- Declare an array of size 3×3.
- Ask the user for input of array elemnts.
- Use two for loops to iterate the rows and columns to input the array elements.
- Show the array to the user.
- Similarly use two for loops to iterate the rows and columns, then calculate the main diagonal sum.
- Repeat the above step to calculate the counter diagonal sum.
- Multiply both of them and 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); // Initializing the 3X3 matrix i.e. 2D array int arr[][] = new int[3][3]; System.out.print("Enter the 3x3 matrix elements :"); int row, col, mainSum = 0, counterSum = 0; // Loop to take user input for(row=0;row<3;row++) for(col=0;col<3;col++) arr[row][col] = scan.nextInt(); System.out.println("\nThe array 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]+" "); } } System.out.print("\n"); // Loop to take the sum of main diagonal elements for(row=0;row<3;row++) for(col=0;col<3;col++) { if(row==col) mainSum+=arr[row][col]; // if(row+col==2) } System.out.println("Sum of counter diagonal : "+mainSum); // Loop to take the sum of counter diagonal elements for(row=0;row<3;row++) for(col=0;col<3;col++) { if(row+col==2) counterSum+=arr[row][col]; } System.out.println("Sum of counter diagonal : "+counterSum); // Printing product betweeen both diagonals sum System.out.print("\nDifference between sum of both diagonal : "+(mainSum*counterSum)); } }
Output: Enter the 3x3 matrix elements : 1 2 3 4 5 6 7 8 9 The array elements are : 1 2 3 4 5 6 7 8 9 Sum of counter diagonal : 15 Sum of counter diagonal : 15 Difference between sum of both diagonal : 225
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Related Java Programs: