// Sum of Right and Left Diagonal Elements present in Matrix
import java.io.*;
public class SumOfDiagonalElements
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][];
int i, j, sum = 0;
// Read Dimension of Square Matrix from User
System.out.print("Enter Dimension of Square Matrix:: ");
int n = Integer.parseInt(br.readLine());
// Note: Here, we are reading only one dimension from user
// Because to obtain diagonal, we need a square matrix.
// You can also read other dimension from user and perform addition
// accordingly
a = new int[n][n];
// Read Matrix Elements from User
System.out.println("Enter Matrix Data::");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("Enter Element:: ");
a[i][j] = Integer.parseInt(br.readLine());
}
}
// Displaying Matrix
System.out.println("\nMatrix is::");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("\t" + a[i][j]);
}
System.out.println();
}
// Perform Sum of Diagonal Elements
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (j == i || j == ((n - 1) - 1))
sum = sum + a[i][j];
// Display Result
System.out.println("\nSum of Diagonal Elements In Matrix:: " + sum);
}
}
// Note: Dimension of Matrix => Rows x Columns of Matrix
OUTPUT::
Enter Dimension of Square Matrix:: 3
Enter Matrix Data::
Enter Element:: 1
Enter Element:: 2
Enter Element:: 3
Enter Element:: 4
Enter Element:: 5
Enter Element:: 6
Enter Element:: 7
Enter Element:: 8
Enter Element:: 9
Matrix is::
1 2 3
4 5 6
7 8 9
Sum of Diagonal Elements In Matrix:: 25
import java.io.*;
public class SumOfDiagonalElements
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a[][];
int i, j, sum = 0;
// Read Dimension of Square Matrix from User
System.out.print("Enter Dimension of Square Matrix:: ");
int n = Integer.parseInt(br.readLine());
// Note: Here, we are reading only one dimension from user
// Because to obtain diagonal, we need a square matrix.
// You can also read other dimension from user and perform addition
// accordingly
a = new int[n][n];
// Read Matrix Elements from User
System.out.println("Enter Matrix Data::");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("Enter Element:: ");
a[i][j] = Integer.parseInt(br.readLine());
}
}
// Displaying Matrix
System.out.println("\nMatrix is::");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
System.out.print("\t" + a[i][j]);
}
System.out.println();
}
// Perform Sum of Diagonal Elements
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (j == i || j == ((n - 1) - 1))
sum = sum + a[i][j];
// Display Result
System.out.println("\nSum of Diagonal Elements In Matrix:: " + sum);
}
}
// Note: Dimension of Matrix => Rows x Columns of Matrix
OUTPUT::
Enter Dimension of Square Matrix:: 3
Enter Matrix Data::
Enter Element:: 1
Enter Element:: 2
Enter Element:: 3
Enter Element:: 4
Enter Element:: 5
Enter Element:: 6
Enter Element:: 7
Enter Element:: 8
Enter Element:: 9
Matrix is::
1 2 3
4 5 6
7 8 9
Sum of Diagonal Elements In Matrix:: 25