Question: Program to check whether the given matrix is Diagonal or not.
A matrix is said to be diagonal matrix when it satisfied the following conditions :
For Example,
the above three diagonal matrices are the order of 1,2,3 respectively.
Code Snippet:
A matrix is said to be diagonal matrix when it satisfied the following conditions :
- Order of matrix must be same i.e (3x3) or (4x4) or (2x2) and so on.
- Matrix is square matrix also.
- All the non-diagonal element of that matrix is zero.
- the diagonal of the matrix is the non-zero elements.
For Example,
Diagonal Matrices |
the above three diagonal matrices are the order of 1,2,3 respectively.
Code Snippet:
- using System;
- public class DiagMat
- {
- public static void Main()
- {
- int[,] matA = new int[10,10];
- bool IsDiag = false;
- Console.WriteLine("\nFor Diagonal Matrix Same no. of row and column required.");
- Console.Write("\nEnter the no. of row. ");
- int row = Convert.ToInt32(Console.ReadLine());
- Console.Write("Enter the no. of col. ");
- int col = Convert.ToInt32(Console.ReadLine());
- // enter the element of the matrix
- Console.WriteLine("\nEnter the element");
- for(int i = 1; i<=row; i++)
- {
- for(int j = 1; j<=col; j++)
- {
- Console.Write("Matrix A[{0},{1}]:", i,j);
- matA[i,j]= Convert.ToInt32(Console.ReadLine());
- }
- Console.WriteLine();
- }
- // Print the matrix
- Console.WriteLine("\nMatrix A: ");
- for(int i = 1; i<=row; i++)
- {
- for(int j = 1; j<=col; j++)
- {
- Console.Write("{0} ", matA[i,j]);
- }
- Console.WriteLine();
- }
- Console.ReadLine();
- // Check weather the matrix is diagonal or not
- for(int i = 1; i<=row; i++)
- {
- for(int j = 1; j<=col; j++)
- {
- if((i != j) && (matA[i,j] == 0))
- {
- IsDiag = true;
- }
- }
- Console.WriteLine();
- }
- if(IsDiag == true)
- {
- Console.WriteLine("Diagonal matrix");
- }
- else
- {
- Console.WriteLine("Not a Diagonal matrix");
- }
- }
- }
** Complete code
Description:
In the above code first of all we enter the no. of row and column of the matrix (which must be equal) in the line 10 to 14 after that we enter element of the matrix from Line 17 to 26 after that we print the entered matrix and after that we check weather the matrix is diagonal or not to do this we take a bool variable IsDiag and initialize it to false.
In the above code first of all we enter the no. of row and column of the matrix (which must be equal) in the line 10 to 14 after that we enter element of the matrix from Line 17 to 26 after that we print the entered matrix and after that we check weather the matrix is diagonal or not to do this we take a bool variable IsDiag and initialize it to false.
- for(int i = 1; i<=row; i++)
- {
- for(int j = 1; j<=col; j++)
- {
- if((i != j) && (matA[i,j] == 0))
- {
- IsDiag = true;
- }
- }
- Console.WriteLine();
- }
We check a condition in Line 05 that if i is not equal to j and if element of matrix matA[i,j] is zero then the IsDiag condition changed to true. when the loop is completed we go for the next step to print the statement, Matrix is diagonal or not. we do this in the following code
- if(IsDiag == true)
- {
- Console.WriteLine("Diagonal matrix");
- }
- else
- {
- Console.WriteLine("Not a Diagonal matrix");
- }
in the above code if the IsDiag is equals to true thant the matrix is diagonal matrix otherwise it is not a diagonal matrix.