0% found this document useful (0 votes)
2 views

Program 16

Uploaded by

Jaainam Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program 16

Uploaded by

Jaainam Jain
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Program 16 Symmetric matrix

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 I th 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.util.*;
class Symmetric
{
public static void main ()
{
Scanner sc=new Scanner(System. in);
int i,j,flag=0,sum=0;
System.out.print("Enter the value of M:");
int M=sc.nextInt();
int x[][]=new int[M][M];
if(M>2 && M<10)
{
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
System.out.print("Enter any value:");
x[i][j]=sc.nextInt();
}
}
System.out.println("ORIGINAL MATRIX");
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
System.out.print(x[i][j]+”\t”);
}
System.out.println();
}
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
if(x[i][j]!= x[i][j])
{
flag=1;
}
}
}
if(flag==0)
{
System.out.println("THE GIVEN MATRIX IS SYMMETRIC");
}
else
{
System.out.println("THE GIVEN MATRIX IS NOT SYMMETRIC");
}
//SUM OF LEFT DIAGONAL
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
if(i==j)
{
sum=sum+x[i][j];
}
}
}
System.out.println("The Sum of the left diagonal="+sum);
//SUM OF RIGHT DIAGONAL
Sum=0
for(i=0;i<M;i++)
{
for(j=0;j<M;j++)
{
if(i+j==M-1)
{
sum=sum+x[i][j];
}
}
}
System.out.println("The Sum of the Right diagonal="+sum);
}
else
{
System.out. println("THE MATRIX SIZE IS OUT OF RANGE”);
}
}
}

You might also like