programa Java para comprobar si una matriz es simétrica o no

En este programa java, estamos leyendo una matriz y comprobar si matriz de entrada es una matriz simétrica o no ?

Dada una matriz y tenemos que comprobar si es o no simétrica utilizando el programa Java?

matriz simétrica

Si la suma de la diagonal diagonal y derecha izquierda de la matriz es igual entonces la matriz anterior se dice que es matriz simétrica. Las entradas de una matriz simétrica son simétricas con respecto a la diagonal class.

Ejemplo:

    Enter the number of elements : 3
Inputting the elements of the Matrix : n
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
The Original Matrix is :
11 11 11
11 11 11
11 11 11
The given Matrix is Symmetric
The sum of the left diagonal = 33
The sum of the right diagonal = 33

programa para comprobar matriz es simétrica o no en java

// This program will find weather the matrix is symmetric or not.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ExArrayFindSymmetric
{
public static void main(String args[])throws IOException
{
// create buffer class object.
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
// enter how many elements you have to enter.
System.out.print("Enter the number of elements : ");
int m=Integer.parseInt(br.readLine());
int A[][]=new int[m][m];
// Check for rows and column.
if(m>2 && m<10)
{
// enter the elements here.
System.out.println("nInputting the elements of 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());
}
}
// here 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();
}
// check weather 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])
{
// When elements do not matched.
flag = 1;
break;
}
}
}
// check for symmetric or not.
if(flag == 1)
System.out.println("nThe given Matrix is Not Symmetric");
else
System.out.println("nThe given Matrix is Symmetric");
// calculate sum of diagonals.
int ld = 0, rd = 0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
// for the left diagonal
if(i == j)
{
ld = ld + A[i][j];
}
// for the right diagonal
if((i+j) == (m-1))
{
rd = rd + A[i][j];
}
}
}
// print sum of left and right diagonals.
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....");
}
}

salida

First run:
Enter the number of elements : 3
Inputting the elements of the Matrix : n
Enter the elements : 11
Enter the elements : 12
Enter the elements : 12
Enter the elements : 55
Enter the elements : 65
Enter the elements : 66
Enter the elements : 45
Enter the elements : 25
Enter the elements : 35
The Original Matrix is :
11 12 12
55 65 66
45 25 35
The given Matrix is Not Symmetric
The sum of the left diagonal = 111
The sum of the right diagonal = 122
Second run:
Enter the number of elements : 3
Inputting the elements of the Matrix : n
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
Enter the elements : 11
The Original Matrix is :
11 11 11
11 11 11
11 11 11
The given Matrix is Symmetric
The sum of the left diagonal = 33
The sum of the right diagonal = 33
Third run:
Enter the number of elements : 2
The Matrix Size is Out Of Range....


Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *