Title: Calculate the sum of diagonals in a matrix
In this blog post, we’ll delve into the world of matrices and explore how to calculate the sum of diagonals in a 3×3 matrix using Java. We’ll provide a Java program with detailed comments and explanations, along with examples to illustrate the concept. By the end, you’ll have a clear understanding of how to perform this operation efficiently in Java.
Understanding the 3×3 Matrix
A 3×3 matrix is a fundamental structure in linear algebra, represented as:
[latex] \begin{bmatrix} a & b & c \\ d & e & f \\ g & h & i \end{bmatrix} [/latex]
In this matrix, we have two main diagonals: the principal diagonal (from the top-left to the bottom-right) and the secondary diagonal (from the top-right to the bottom-left). Our goal is to calculate the sum of elements along these diagonals.
Java Program for Diagonal Sum Calculation
Let’s dive into the Java program that calculates the sum of diagonals in a 3×3 matrix:
public class DiagonalSum3x3Matrix {
public static void main(String[] args) {
// Initialize the 3x3 matrix
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Initialize variables to hold sum of principal and secondary diagonals
int principalSum = 0;
int secondarySum = 0;
// Loop through the rows of the matrix
for (int i = 0; i < matrix.length; i++) {
// Add elements of principal diagonal (from top-left to bottom-right)
principalSum += matrix[i][i];
// Add elements of secondary diagonal (from top-right to bottom-left)
secondarySum += matrix[i][matrix.length - 1 - i];
}
// Display the sums of principal and secondary diagonals
System.out.println("Sum of Principal Diagonal: " + principalSum);
System.out.println("Sum of Secondary Diagonal: " + secondarySum);
}
}Explanation of the Java Program
- We start by initializing our 3×3 matrix
matrixwith values from 1 to 9. - Two variables
principalSumandsecondarySumare used to store the sums of the principal and secondary diagonals, respectively. - We then enter a
forloop that iterates through each row of the matrix. - For the principal diagonal, we add the element at the position
[i][i]toprincipalSum. - For the secondary diagonal, we add the element at the position
[i][matrix.length - 1 - i]tosecondarySum.
Example Calculation
Let’s consider the following 3×3 matrix:
[latex] \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} [/latex]
- Principal Diagonal Sum: ( 1 + 5 + 9 = 15 )
- Secondary Diagonal Sum: ( 3 + 5 + 7 = 15 )
Learn and practice more on Array Programs: Array
Conclusion
In this blog post, we’ve explored how to calculate the sum of diagonals in a 3×3 matrix using Java. The program efficiently computes the sums of the principal and secondary diagonals, demonstrating a fundamental matrix operation.
- Partition Array into Two Equal Sum SubsetsTitle: Partition Array into Two Equal Sum Subsets In this guide, you will learn how to check whether a Java… Read more: Partition Array into Two Equal Sum Subsets
- Merge K Sorted ArraysTitle: Merge K Sorted Arrays In this guide, you’ll learn how to merge k sorted arrays into a single sorted… Read more: Merge K Sorted Arrays
- Merge Two Arrays without DuplicatesTitle: Merge Two Arrays without Duplicates In this guide, you will learn how to merge two arrays in Java while… Read more: Merge Two Arrays without Duplicates
- Find Symmetric Difference of Two ArraysTitle: Find Symmetric Difference of Two Arrays In this guide, you will learn how to find the symmetric difference of… Read more: Find Symmetric Difference of Two Arrays
- Find Difference of Two ArraysTitle: Find Difference of Two Arrays In this guide, you will learn how to find the difference between two arrays… Read more: Find Difference of Two Arrays
- Find Intersection of Two ArraysTitle: Find Intersection of Two Arrays In this guide, you’ll learn how to find the intersection of two arrays in… Read more: Find Intersection of Two Arrays



