Problem Statement:- Program to check whether a matrix is symmetric or not.
Definition of symmetric Matrix:- A Square matrix A is called a symmetric matrix A=AT.
Example:-
Sample Input/Output:-
Data requirement:-
Input Data:- row_size, col_size, matrix[][]
Output Data:-String output
Additional Data:- i, j, flag
Program in C
Here is the source code of the C Program to check whether a matrix is symmetric or not.
Code:
#include<stdio.h>
int main()
{
int row_size,col_size;
//Get size of matrix
printf("Enter the row Size Of the Matrix:");
scanf("%d",&row_size);
printf("Enter the columns Size Of the Matrix:");
scanf("%d",&col_size);
int matrix[row_size][col_size];
//Taking input of the matrix
int i,j;
printf("Enter the Matrix Element:\n");
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
scanf("%d",&matrix[i][j]);
}
}
if(row_size!=col_size)
{
printf("Given Matrix is not a Square Matrix.");
}
else{
//compute the transpose matrix
int tran_matrix[col_size][row_size];
for(i=0;i<col_size;i++)
{
for(j=0;j<row_size;j++)
{
tran_matrix[i][j]=matrix[j][i];
}
}
/*check given matrix elements and transpose
matrix elements are same or not.*/
int flag=0;
for(i=0;i<col_size;i++)
{
for(j=0;j<row_size;j++)
{
if(matrix[i][j]!=tran_matrix[i][j])
{
flag=1;
break;
}
}
}
if(flag==1)
printf("Given Matrix is not a symmetric Matrix.");
else
printf("Given Matrix is a symmetric Matrix.");
}
}
Input/Output:
Enter the row Size Of the Matrix:2
Enter the columns Size Of the Matrix:2
Enter the Matrix Element:
1 2
2 3
Given Matrix is a symmetric Matrix.
Program in C++
#include<stdio.h> int main() { int row_size,col_size; //Get size of matrix printf("Enter the row Size Of the Matrix:"); scanf("%d",&row_size); printf("Enter the columns Size Of the Matrix:"); scanf("%d",&col_size); int matrix[row_size][col_size]; //Taking input of the matrix int i,j; printf("Enter the Matrix Element:\n"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { scanf("%d",&matrix[i][j]); } } if(row_size!=col_size) { printf("Given Matrix is not a Square Matrix."); } else{ //compute the transpose matrix int tran_matrix[col_size][row_size]; for(i=0;i<col_size;i++) { for(j=0;j<row_size;j++) { tran_matrix[i][j]=matrix[j][i]; } } /*check given matrix elements and transpose matrix elements are same or not.*/ int flag=0; for(i=0;i<col_size;i++) { for(j=0;j<row_size;j++) { if(matrix[i][j]!=tran_matrix[i][j]) { flag=1; break; } } } if(flag==1) printf("Given Matrix is not a symmetric Matrix."); else printf("Given Matrix is a symmetric Matrix."); } }
Here is the source code of the C++ Program to check whether a matrix is symmetric or not.
Code:
#include<iostream>
using namespace std;
int main()
{
int row_size,col_size;
//Get size of matrix
cout<<"Enter the row Size Of the Matrix:";
cin>>row_size;
cout<<"Enter the columns Size Of the Matrix:";
cin>>col_size;
int matrix[row_size][col_size];
//Taking input of the matrix
int i,j;
cout<<"Enter the Matrix Element:\n";
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
cin>>matrix[i][j];
}
}
if(row_size!=col_size)
{
cout<<"Given Matrix is not a Square Matrix.";
}
else{
//compute the transpose matrix
int tran_matrix[col_size][row_size];
for(i=0;i<col_size;i++)
{
for(j=0;j<row_size;j++)
{
tran_matrix[i][j]=matrix[j][i];
}
}
/*check given matrix elements and transpose
matrix elements are same or not.*/
int flag=0;
for(i=0;i<col_size;i++)
{
for(j=0;j<row_size;j++)
{
if(matrix[i][j]!=tran_matrix[i][j])
{
flag=1;
break;
}
}
}
if(flag==1)
cout<<"Given Matrix is not a symmetric Matrix.";
else
cout<<"Given Matrix is a symmetric Matrix.";
}
}
#include<iostream> using namespace std; int main() { int row_size,col_size; //Get size of matrix cout<<"Enter the row Size Of the Matrix:"; cin>>row_size; cout<<"Enter the columns Size Of the Matrix:"; cin>>col_size; int matrix[row_size][col_size]; //Taking input of the matrix int i,j; cout<<"Enter the Matrix Element:\n"; for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { cin>>matrix[i][j]; } } if(row_size!=col_size) { cout<<"Given Matrix is not a Square Matrix."; } else{ //compute the transpose matrix int tran_matrix[col_size][row_size]; for(i=0;i<col_size;i++) { for(j=0;j<row_size;j++) { tran_matrix[i][j]=matrix[j][i]; } } /*check given matrix elements and transpose matrix elements are same or not.*/ int flag=0; for(i=0;i<col_size;i++) { for(j=0;j<row_size;j++) { if(matrix[i][j]!=tran_matrix[i][j]) { flag=1; break; } } } if(flag==1) cout<<"Given Matrix is not a symmetric Matrix."; else cout<<"Given Matrix is a symmetric Matrix."; } }
Input/Output:
Enter the row Size Of the Matrix:3
Enter the columns Size Of the Matrix:3
Enter the Matrix Element:
5 9 3
9 6 4
3 4 2
Given Matrix is a symmetric Matrix.
Program in Java
Here is the source code of the Java Program to check whether a matrix is symmetric or not.
Code:
import java.util.Scanner;
public class CheckSymmetricMatrix {
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int row_size,col_size;
//Get size of matrix
System.out.print("Enter the row Size Of the Matrix:");
row_size=cs.nextInt();
System.out.print("Enter the columns Size Of the Matrix:");
col_size=cs.nextInt();
int matrix[][] =new int[row_size][col_size];
//Taking input of the matrix
int i,j;
System.out.println("Enter the Matrix Element:");
for(i=0;i<row_size;i++)
{
for(j=0;j<col_size;j++)
{
matrix[i][j]=cs.nextInt();
}
}
if(row_size!=col_size)
{
System.out.print("Given Matrix is not a Square Matrix.");
}
else{
//compute the transpose matrix
int tran_matrix[][]=new int[col_size][row_size];
for(i=0;i<col_size;i++)
{
for(j=0;j<row_size;j++)
{
tran_matrix[i][j]=matrix[j][i];
}
}
/*check given matrix elements and transpose
matrix elements are same or not.*/
int flag=0;
for(i=0;i<col_size;i++)
{
for(j=0;j<row_size;j++)
{
if(matrix[i][j]!=tran_matrix[i][j])
{
flag=1;
break;
}
}
}
if(flag==1)
System.out.print("Given Matrix is not a symmetric Matrix.");
else
System.out.print("Given Matrix is a symmetric Matrix.");
}
cs.close();
}
}
Input/Output:
Enter the row Size Of the Matrix:3
Enter the columns Size Of the Matrix:2
Enter the Matrix Element:
4 5
7 8
9 3
Given Matrix is not a Square Matrix.
Program in Python
import java.util.Scanner; public class CheckSymmetricMatrix { public static void main(String[] args) { Scanner cs=new Scanner(System.in); int row_size,col_size; //Get size of matrix System.out.print("Enter the row Size Of the Matrix:"); row_size=cs.nextInt(); System.out.print("Enter the columns Size Of the Matrix:"); col_size=cs.nextInt(); int matrix[][] =new int[row_size][col_size]; //Taking input of the matrix int i,j; System.out.println("Enter the Matrix Element:"); for(i=0;i<row_size;i++) { for(j=0;j<col_size;j++) { matrix[i][j]=cs.nextInt(); } } if(row_size!=col_size) { System.out.print("Given Matrix is not a Square Matrix."); } else{ //compute the transpose matrix int tran_matrix[][]=new int[col_size][row_size]; for(i=0;i<col_size;i++) { for(j=0;j<row_size;j++) { tran_matrix[i][j]=matrix[j][i]; } } /*check given matrix elements and transpose matrix elements are same or not.*/ int flag=0; for(i=0;i<col_size;i++) { for(j=0;j<row_size;j++) { if(matrix[i][j]!=tran_matrix[i][j]) { flag=1; break; } } } if(flag==1) System.out.print("Given Matrix is not a symmetric Matrix."); else System.out.print("Given Matrix is a symmetric Matrix."); } cs.close(); } }
Here is the source code of the Python Program to check whether a matrix is symmetric or not.
Code:
# Get size of matrix
row_size=int(input("Enter the row Size Of the Matrix:"))
col_size=int(input("Enter the columns Size Of the Matrix:"))
matrix=[]
# Taking input of the 1st matrix
print("Enter the Matrix Element:")
for i in range(row_size):
matrix.append([int(j) for j in input().split()])
if row_size!=col_size:
print("Given Matrix is not a Square Matrix.")
else:
#compute the transpose matrix
tran_matrix = [[0 for i in range(col_size)] for i in range(row_size)]
for i in range(0, row_size):
for j in range(0, col_size):
tran_matrix[i][j] = matrix[j][i]
# check given matrix elements and transpose
# matrix elements are same or not.
flag=0
for i in range(0, row_size):
for j in range(0, col_size):
if matrix[i][j] != tran_matrix[i][j]:
flag=1
break
if flag==1:
print("Given Matrix is not a symmetric Matrix.")
else:
print("Given Matrix is a symmetric Matrix.")
Input/Output:
Enter the row Size Of the Matrix:3
Enter the columns Size Of the Matrix:3
Enter the Matrix Element:
4 5 6
7 8 9
2 3 4
Given Matrix is not a symmetric Matrix.
# Get size of matrix row_size=int(input("Enter the row Size Of the Matrix:")) col_size=int(input("Enter the columns Size Of the Matrix:")) matrix=[] # Taking input of the 1st matrix print("Enter the Matrix Element:") for i in range(row_size): matrix.append([int(j) for j in input().split()]) if row_size!=col_size: print("Given Matrix is not a Square Matrix.") else: #compute the transpose matrix tran_matrix = [[0 for i in range(col_size)] for i in range(row_size)] for i in range(0, row_size): for j in range(0, col_size): tran_matrix[i][j] = matrix[j][i] # check given matrix elements and transpose # matrix elements are same or not. flag=0 for i in range(0, row_size): for j in range(0, col_size): if matrix[i][j] != tran_matrix[i][j]: flag=1 break if flag==1: print("Given Matrix is not a symmetric Matrix.") else: print("Given Matrix is a symmetric Matrix.")
0 Comments
Please do not Enter any spam link in the comment box