Symmetric Matrix Program in Java and Python
Symmetric matrix program with diagonal sums, algorithm, explanation, Java solution and simple Python solution for ISC students.
Question:
Write a program to declare a square matrix A[][] of order M × M, where M must be greater than 2 and less than 10. Accept M and the matrix elements from the user.
Display the original matrix, check whether the matrix is symmetric, and find the sums of the left and right diagonals.
A square matrix is symmetric when the element of the ith row and jth column is equal to the element of the jth row and ith column.
Algorithm:
Step 1: Start.
Step 2: Accept the matrix order M.
Step 3: If M is not greater than 2 and less than 10, display size out of range and stop.
Step 4: Input all elements into matrix A[M][M].
Step 5: Display the original matrix using nested loops.
Step 6: Initialize a flag to 0 and compare A[i][j] with A[j][i] for required positions.
Step 7: If any pair is unequal, set the flag to show that the matrix is not symmetric.
Step 8: Initialize left diagonal sum and right diagonal sum to 0.
Step 9: Add A[i][i] to the left diagonal sum for each row i.
Step 10: Add A[i][M - 1 - i] to the right diagonal sum for each row i.
Step 11: Display whether the matrix is symmetric and print both diagonal sums.
Step 12: Stop.
Explanation:
The program works with a square matrix because symmetry across the main diagonal is defined only for square matrices. It first validates the order of the matrix according to the question range. If the order is valid, nested loops are used to input every element into A[i][j] and another pair of nested loops displays the original matrix.
To test symmetry, the program compares corresponding elements across the main diagonal. For a symmetric matrix, every A[i][j] must be equal to A[j][i]. The nested loops check these pairs. If even one unequal pair is found, flag is set to 1. This flag is useful because a single mismatch is enough to prove that the matrix is not symmetric. The program can then print the result based on the value of the flag.
The program also calculates diagonal sums. The left diagonal uses positions where row and column indexes are equal, so the elements are A[i][i]. The right diagonal begins at the top-right corner and moves toward the bottom-left corner, so its column index decreases as the row index increases. These two sums are maintained separately. Thus the program does three connected matrix tasks: input/display, symmetry checking, and diagonal-sum calculation.
This structure also helps avoid mixing the two ideas of symmetry and diagonal sums. Symmetry depends on paired positions across the diagonal, while diagonal sums depend only on specific diagonal paths. Keeping these checks separate makes the program easier to trace and reduces logical mistakes.
Java Program:
/**
* The class SymmetricMatrix_ISC2014 inputs a 2D array and checks whether it is Symmetric or not.
* It then finds the sum of the left and the right diagonals
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2014 Question 2
*/
import java.util.Scanner;
class SymetricMatrix_ISC2014
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements : ");
int m=sc.nextInt();
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]=sc.nextInt();
}
}
/* 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");
}
}Equivalent Python Program:
# Read the matrix or array size and store the values for indexed processing.
# Nested loops are used because each row/column or array position must be checked.
# Print the processed array or matrix in the required output format.
m = int(input("M = "))
if m <= 2 or m >= 10:
print("THE MATRIX SIZE IS OUT OF RANGE")
else:
A = []
print("Enter the elements:")
for i in range(m):
row = []
for j in range(m):
row.append(int(input()))
A.append(row)
print("ORIGINAL MATRIX")
for i in range(m):
for j in range(m):
print(A[i][j], end=" ")
print()
flag = 0
for i in range(m):
for j in range(m):
if A[i][j] != A[j][i]:
flag = 1
left_sum = 0
right_sum = 0
for i in range(m):
left_sum = left_sum + A[i][i]
right_sum = right_sum + A[i][m - 1 - i]
if flag == 0:
print("THE GIVEN MATRIX IS SYMMETRIC")
else:
print("THE GIVEN MATRIX IS NOT SYMMETRIC")
print("The sum of the left diagonal =", left_sum)
print("The sum of the right diagonal =", right_sum)Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.