Diagonal Matrix Program in Java and Python
Diagonal Matrix program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.
Question:
Write a program to input a 2-D square matrix and check whether it is a Diagonal Matrix or not.
Diagonal Matrix : A diagonal matrix is a matrix (usually a square matrix) in which the entries outside the main diagonal (↘) are all zero. The diagonal entries themselves may or may not be zero (but all diagonal entries cannot be zero).
Example:
\[\begin{bmatrix} 5 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 7 \end{bmatrix}\]
Algorithm:
Step 1: Start.
Step 2: Accept the order of the square matrix.
Step 3: Input all elements of the matrix.
Step 4: Display the matrix.
Step 5: Initialize two counters: one for non-zero non-diagonal elements and one for zero diagonal elements.
Step 6: Read every element of the matrix.
Step 7: If an element outside the main diagonal is non-zero, mark the matrix as not diagonal.
Step 8: If an element on the main diagonal is zero, count it.
Step 9: If all non-diagonal elements are zero and all diagonal elements are not zero, display that the matrix is diagonal; otherwise, display that it is not diagonal.
Step 10: Use the condition i != j to identify non-diagonal positions.
Step 11: Change the flag immediately if any non-diagonal position contains a non-zero value.
Step 12: Stop.
Explanation:
A diagonal matrix may contain any values on the main diagonal, but all non-diagonal elements must be zero.
The program checks the matrix positions using row and column indexes. When the row index and column index are equal, the element lies on the main diagonal and does not need to be zero.
When the row and column indexes are different, the element is outside the main diagonal. If any such element is non-zero, the matrix cannot be diagonal.
A flag stores whether a wrong non-diagonal value has been found. This makes the final decision simple after all required positions have been checked.
The row and column indexes are the key to the logic. The condition i != j identifies every non-diagonal position, so the program can ignore diagonal values and focus only on values that must be zero.
A diagonal matrix allows any values on the main diagonal but requires all other positions to be zero. The main diagonal is where row and column indexes are equal. Therefore, the program mainly checks positions where i != j. If any such position contains a non-zero value, the matrix cannot be diagonal. This logic is more precise than checking all elements together because diagonal elements are not restricted to a particular value. The program separates allowed positions from restricted positions using indexes.
Java Program:
/**
* The class DiagonalMatrix inputs a Matrix and checks whether it is a diagonal matrix or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.*;
class DiagonalMatrix
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();
int A[][]=new int[m][m];
/* Inputting the matrix */
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}
}
/* Printing the matrix */
System.out.println("*************************");
System.out.println("The 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();
}
System.out.println("*************************");
int p=0, q=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
if(i!=j && A[i][j]!=0) // Checking non-diagonal elements
{
p=1;
break;
}
if(i==j && A[i][j]==0) // Checking diagonal elements
{
q++;
}
}
}
if(p==0 && q<m)
System.out.println("The matrix is Diagonal");
else
System.out.println("The matrix is not Diagonal");
}
}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("Enter the size of the matrix: "))
A = []
for i in range(m):
row = []
for j in range(m):
row.append(int(input("Enter an element: ")))
A.append(row)
print("The Matrix is:")
for i in range(m):
for j in range(m):
print(A[i][j], end=" ")
print()
p = 0
q = 0
for i in range(m):
for j in range(m):
if i != j and A[i][j] != 0:
p = 1
if i == j and A[i][j] == 0:
q = q + 1
if p == 0 and q < m:
print("The matrix is Diagonal")
else:
print("The matrix is not Diagonal")Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.