Check and print unit matrix using Array in C


This program is about checking the given 2D matrix is unit matrix or not using for loops and if conditions.

Source Code

#include<stdio.h>
int main()
{
  int a[10][10],i,j,one=0,zero=0,r,c;
  printf("\nEnter No of Rows:");
  scanf("%d",&r);
  printf("\nEnter No of Columns:");
  scanf("%d",&c);
  printf("\nEnter the Matrix Values:");
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
      scanf("\n%d",&a[i][j]);
    }
  }
  for(i=0;i<r;i++)
  {
    for(j=0;j<c;j++)
    {
      if(i==j)
      {
        if(a[i][j]==1)
        {
           one++;
        }
      }
      else
      {
        if(a[i][j]==0)
        {
           zero++;
        }
      }
    }
  }
  if((r*c)==(one+zero))
  {
    printf("%d*%d Unit matrix",r,c);
  }
  else
  {
    printf("%d*%d Not Unit matrix",r,c);
  }
  return 0;
}
  
To download raw file Click Here

Output

Enter No of Rows:3
Enter No of Columns:3
Enter the Matrix Values:1
0
0
0
1
0
0
0
1
3*3 Unit Matrix


List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C


Learn All in Tamil © Designed & Developed By Tutor Joes