Sunday 22 March 2015

Diagonal Matrix in C#

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 :

  • 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,
Example of diagonal matrices
Diagonal Matrices


the above three diagonal matrices are the order of 1,2,3 respectively.

Code Snippet:

  1. using System;    
  2. public class DiagMat    
  3. {    
  4.     public static void Main()    
  5.     {    
  6.         int[,] matA = new int[10,10];    
  7.         bool IsDiag = false;    
  8.             
  9.         Console.WriteLine("\nFor Diagonal Matrix Same no. of row and column required.");    
  10.         Console.Write("\nEnter the no. of row. ");    
  11.         int row = Convert.ToInt32(Console.ReadLine());    
  12.         
  13.         Console.Write("Enter the no. of col. ");    
  14.         int col = Convert.ToInt32(Console.ReadLine());    
  15.     
  16.                 // enter the element of the matrix    
  17.         Console.WriteLine("\nEnter the element");    
  18.         for(int i = 1; i<=row; i++)    
  19.         {    
  20.             for(int j = 1; j<=col; j++)    
  21.             {    
  22.                 Console.Write("Matrix A[{0},{1}]:", i,j);    
  23.                 matA[i,j]= Convert.ToInt32(Console.ReadLine());    
  24.             }    
  25.                 Console.WriteLine();    
  26.         }    
  27.     
  28.                 // Print the matrix    
  29.     
  30.         Console.WriteLine("\nMatrix A: ");    
  31.         for(int i = 1; i<=row; i++)    
  32.         {    
  33.             for(int j = 1; j<=col; j++)    
  34.             {    
  35.                 Console.Write("{0} ", matA[i,j]);    
  36.             }    
  37.                 Console.WriteLine();    
  38.         }    
  39.         Console.ReadLine();    
  40.     
  41.                // Check weather the matrix is diagonal or not    
  42.     
  43.         for(int i = 1; i<=row; i++)    
  44.         {    
  45.             for(int j = 1; j<=col; j++)    
  46.             {    
  47.                 if((i != j) && (matA[i,j] == 0))    
  48.                 {    
  49.                     IsDiag = true;    
  50.                 }    
  51.             }    
  52.                 Console.WriteLine();    
  53.         }    
  54.     
  55.         if(IsDiag == true)    
  56.         {    
  57.             Console.WriteLine("Diagonal matrix");    
  58.         }    
  59.         else    
  60.         {    
  61.             Console.WriteLine("Not a Diagonal matrix");    
  62.         }       
  63.     }    
  64. }
     ** 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.


  1. for(int i = 1; i<=row; i++)    
  2.         {    
  3.             for(int j = 1; j<=col; j++)    
  4.             {    
  5.                 if((i != j) && (matA[i,j] == 0))    
  6.                 {    
  7.                     IsDiag = true;    
  8.                 }    
  9.             }    
  10.                 Console.WriteLine();    
  11.         } 

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


  1. if(IsDiag == true)    
  2.         {    
  3.             Console.WriteLine("Diagonal matrix");    
  4.         }    
  5.         else    
  6.         {    
  7.             Console.WriteLine("Not a Diagonal matrix");    
  8.         }  

in the above code if the IsDiag is equals to true thant the matrix is diagonal matrix otherwise it is not a diagonal matrix.

Screenshot of output:


Diagonal matrix screenshot
Screenshot of Output