SYMMETRIC MATRIX | ISC COMPUTER 2024 | survnor.blogspot.com
Write a program to declare square matrix A[][] of
order MxM where ‘A’ is the number of rows and the number of columns such that M
must be greater than two 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 mask:--
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 ithcolumn.
c)
Find the sum of the elements of the left
diagonal and the sum of the elements of the right diagonal of the matrix and
display them.
Example 1:- Input:- M=3
Output:- ORIGINAL MATRIX
1 2 3,
2 4 5
3 5 6
ALGORITHM
STEP 1:- Start.
STEP 2:- Declare symmetric as a class.
Declare and initialize variables m, i, j, flagß0, ldß0, rdß0.
STEP 3:- Declare a method FindSymm() with
return type void. Now enter the size of the square matrix and store it into M.
STEP 4:- Now check whether m is greater than
d and less than 10.If condition is satisfied assign the value of m into array
A, otherwise print the message ”THE MATRIX SIZE IS OUT OF RANGE”.
STEP 5:- Now enter the elements in the
matrix using nested loop. Now enter loop on i, starting with 0 and upto less
than M and incremented by 1. Inner loop on ‘j’ starting with 0 and up to less
than ‘M’ and incremented by 1 in each step.
STEP 6:- Now display the array elements in
square matrix from with showing message “ORIGINAL MATRIX”.
STEP 7:- Now check A[i][j]!=A[j][i] within
nested loop of satisfied then showing message “GIVEN MATRIX IS NOT SYMMETRIC”
otherwise showing message “THE GIVEN MATRIX IS SYMMETRIC”.
STEP 8:- Now start iteration for calculating
the sum of left and right diagonal value. Iteration on ‘i’ initialize by 0 and
up to less than M and incremented by 1. Now ldßld+A[i][i] for sum of left diagonal
and rdßrd+A[i][M-1-i]
for sum of right diagonal.
STEP 9:- Now print the sum of left and right
diagonal of a square matrix value.
STEP
10:- Now close
FindSymm().
STEP
11:- Now declare
main() function. Create object ob of class symmetric and call the FindSymm()
through object ob.
STEP
12:- Now close main
function and close class program.
STEP
13:- Stop.
SOURCE CODE
import
java.util.*;
class
symmetric{
int M,i,j,flag=0,ld=0,rd=0;
Scanner sc=new Scanner(System.in);
void FindSymm(){
System.out.println("ENTER THE SIZE
OF MATRIX:");
M=sc.nextInt();
if((M>2)&&(M<10){
int A[][]=new int[M][M];
for(i=0;i<M;i++){
for(j=0;j<M;j++){
A[i][j]=sc.nextInt();}}
System.out.println("ORIGINAL
MATRIX:");
for(i=0;i<M;i++){
for(j=0;j<M;j++){
System.out.print(A[i][j]+"\t");}
System.out.println();}
for(i=0;i<M;i++){
for(j=0;j<M;j++){
if(A[i][j]!=A[j][i]){
flag=1;
break;}}}
if(flag==0)
System.out.println("THE GIVEN
MATRIX IS SYMMETRIC");
else
System.out.println("THE GIVEN MATRIX
IS NOT SYMMETRIC");
for(i=0;i<M;i++){
ld=ld+A[i][i];
rd=rd+A[i][M-1-i];}
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 IS
OUT OF RANGE:");}
public static void main(){
symmetric ob=new symmetric(); ob.FindSymm();}}
VARIABLE DESCRIPTION
S.L.No. |
Name |
Data Type |
Description |
1 |
M |
int |
Use to accept the
value of array A[][] of order MxM. |
2 |
i,j |
int |
These are loop
control variables. |
3 |
flag |
int |
Use for checking
either the matrix is symmetric or not. |
4 |
ld |
int |
Use to store the
sum of left diagonal value. |
5 |
rd |
int |
Use to store the
sum of right diagonal value. |
6 |
A[][] |
int |
Declare as a
square matrix to store the array elements. |
INPUT AND
OUTPUT