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.
- Count Subarrays with Odd SumTitle: Count Subarrays with Odd Sum In this guide, you’ll learn how to count the subarrays of an integer array… Read more: Count Subarrays with Odd Sum
- Count Subarrays Whose Product is Less than KTitle: Count Subarrays Whose Product is Less than K In this guide, you’ll learn how to count the contiguous subarrays… Read more: Count Subarrays Whose Product is Less than K
- Minimum Number of Increments to Make Array Strictly IncreasingTitle: Minimum Number of Increments to Make Array Strictly Increasing In this guide, we will solve a classic greedy array… Read more: Minimum Number of Increments to Make Array Strictly Increasing
- Minimum Number of Operations to Make Array EqualTitle: Minimum Number of Operations to Make Array Equal In this guide, you’ll learn how to solve the Minimum Number… Read more: Minimum Number of Operations to Make Array Equal
- Minimum Number of Deletions to Make an Array SortedTitle: Minimum Number of Deletions to Make an Array Sorted Sorting problems often hide a subtler question: what is the… Read more: Minimum Number of Deletions to Make an Array Sorted
- Minimum Number of Swaps to Bring All Elements Less Than K TogetherTitle: Minimum Number of Swaps to Bring All Elements Less Than K Together In this guide, you’ll learn how to… Read more: Minimum Number of Swaps to Bring All Elements Less Than K Together



