Diagonal Matrix Check


C Program to check whether a given matrix is diagonal matrix.
2    0    0
0    4    0
0    0    6


#include  <stdio.h>
#include  <conio.h>

void main()
{
  int  x[10][10] , nr , nc , r , c , flag ;

  clrscr() ;

  printf("Enter the number of rows and columns: ") ;
  scanf("%d %d", &nr , &nc) ;

  if(nr==nc)  		/* checking for square matrix */
  {
    printf("Enter elements of the matrix: \n") ;
    for(r=0 ; r<nr ; r++)
      for(c=0 ; c<nc ; c++)
        scanf("%d" , &x[r][c]) ;

    flag=1 ;
    for(r=0 ; r<nr ; r++)
      for(c=0 ; c<nc ; c++)
        if(r==c) /* true for diagonal elements */
        {
	       if(x[r][c]==0)
	         flag=0;
        }
        else
        {
	       if(x[r][c]!=0)
	         flag=0;
        }

    if(flag==1)
      printf("The matrix is diagonal") ;
    else
      printf("The matrix is not diagonal") ; 
  }
  else
    printf("The matrix is not a sqaure matrix") ;

  getch() ;
}


Output 1:


Enter the number of rows and columns: 3 3
Enter elements of the matrix: 
11 0 0
0 22 0
0 0 44
The matrix is diagonal


Output 2:


Enter the number of rows and columns: 3 3
Enter elements of the matrix: 
11 0 2
0 22 0
0 0 44
The matrix is not diagonal


Output 3:


Enter the number of rows and columns: 3  3
Enter elements of the matrix: 
0 0 0
0 9 0
0 0 8
The matrix is not diagonal