Write a program to declare a square matrix A[ ] [ ] of order (M x M) where ‘M’ is the number of rows and the number of columns such that M must be greater than 2 and less than 10. Accept the value of M as user input. Display an appropriate message for an invalid input. Allow the user to input integers into this matrix. Perform the following tasks:
(a) Display the original matrix.
(b) Check if the given matrix is Symmetric or not.
A square matrix is said to be Symmetric, if the element of the ith row and jth column is equal to the element of the jth row and ith column.
(c) Find the sum of the elements of left diagonal and the sum of the elements of right diagonal of the matrix and display them.
Test your program with the sample data and some random data:
Example 1
INPUT : M = 3
1 2 3
2 4 5
3 5 6
OUTPUT :
ORIGINAL MATRIX
1 2 3
2 4 5
3 5 6
THE GIVEN MATRIX IS SYMMETRIC
The sum of the left diagonal = 11
The sum of the right diagonal = 10
Example 2
INPUT : M = 4
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
OUTPUT :
ORIGINAL MATRIX
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
THE GIVEN MATRIX IS NOT SYMMETRIC
The sum of the left diagonal = 17
The sum of the right diagonal = 20
Example 3
INPUT : M = 22
OUTPUT : THE MATRIX SIZE IS OUT OF RANGE
import java.io.*;
class SymetricMatrix
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of elements : ");
int m=Integer.parseInt(br.readLine());
int A[][]=new int[m][m];
if(m>2 && m<10) // Checking for valid input of rows and columns size
{
System.out.println("nInputting the elements in the Matrix: n");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter the elements : ");
A[i][j]=Integer.parseInt(br.readLine());
}
} /* Printing the Original Matrix */
System.out.println("nThe Original Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
} /* Checking whether the matrix is symmetric or not */
int flag = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(A[i][j] != A[j][i])
{
flag = 1; // Setting flag = 1 when elements do not match
break;
}
}
}
if(flag == 1)
System.out.println("nThe given Matrix is Not Symmetric");
else
System.out.println("nThe given Matrix is Symmetric");
/* Finding sum of the diagonals */
int ld = 0, rd = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i == j) // Condition for the left diagonal
{
ld = ld + A[i][j];
}
if((i+j) == (m-1)) // Condition for the right diagonal
{
rd = rd + A[i][j];
}
}
}
System.out.println("The sum of the left diagonal = "+ld);
System.out.println("The sum of the right diagonal = "+rd);
}
else
System.out.println("The Matrix Size is Out Of Range");
}
}
(a) Display the original matrix.
(b) Check if the given matrix is Symmetric or not.
A square matrix is said to be Symmetric, if the element of the ith row and jth column is equal to the element of the jth row and ith column.
(c) Find the sum of the elements of left diagonal and the sum of the elements of right diagonal of the matrix and display them.
Test your program with the sample data and some random data:
Example 1
INPUT : M = 3
1 2 3
2 4 5
3 5 6
OUTPUT :
ORIGINAL MATRIX
1 2 3
2 4 5
3 5 6
THE GIVEN MATRIX IS SYMMETRIC
The sum of the left diagonal = 11
The sum of the right diagonal = 10
Example 2
INPUT : M = 4
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
OUTPUT :
ORIGINAL MATRIX
7 8 9 2
4 5 6 3
8 5 3 1
7 6 4 2
THE GIVEN MATRIX IS NOT SYMMETRIC
The sum of the left diagonal = 17
The sum of the right diagonal = 20
Example 3
INPUT : M = 22
OUTPUT : THE MATRIX SIZE IS OUT OF RANGE
import java.io.*;
class SymetricMatrix
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of elements : ");
int m=Integer.parseInt(br.readLine());
int A[][]=new int[m][m];
if(m>2 && m<10) // Checking for valid input of rows and columns size
{
System.out.println("nInputting the elements in the Matrix: n");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter the elements : ");
A[i][j]=Integer.parseInt(br.readLine());
}
} /* Printing the Original Matrix */
System.out.println("nThe Original Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
} /* Checking whether the matrix is symmetric or not */
int flag = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(A[i][j] != A[j][i])
{
flag = 1; // Setting flag = 1 when elements do not match
break;
}
}
}
if(flag == 1)
System.out.println("nThe given Matrix is Not Symmetric");
else
System.out.println("nThe given Matrix is Symmetric");
/* Finding sum of the diagonals */
int ld = 0, rd = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i == j) // Condition for the left diagonal
{
ld = ld + A[i][j];
}
if((i+j) == (m-1)) // Condition for the right diagonal
{
rd = rd + A[i][j];
}
}
}
System.out.println("The sum of the left diagonal = "+ld);
System.out.println("The sum of the right diagonal = "+rd);
}
else
System.out.println("The Matrix Size is Out Of Range");
}
}