Contact Learn C
Copy

Program 133: To find whether given Matrix is Identity or not

Program 133:
 
#include<stdio.h>
main()
{ 
 int i,j,rows,col,flag=1;
 printf("Enter number of rows and columns of a matrix\n");
 scanf("%d %d",&rows,&col);
int a[rows][col];
 if(rows==col)
 {
  //Taking input of matrix
    printf("Enter Matrix 1\n");
    for(i=0;i<rows;i++)
    {
       for(j=0;j<col;j++)
      {
     scanf("%d",&a[i][j]);
      }
    }
    
    printf("Given /matrix is\n");
        for(i=0;i<rows;i++)
         {
         for(j=0;j<col;j++)
         {
           printf("%d\t",a[i][j]);
           }
  
           printf("\n");
           }
         
        for(i=0;i<rows;i++)
        {
         for(j=0;j<col;j++)
         {
          if(i==j &&a[i][j]!=1)
          {
           flag=0;
          }
          if(i!=j && a[i][j]!=0)
          {
           flag=0;
          }
         }
        }
      if(flag==1)
      {
       printf("It is an Identity Matrix\n");
      }
      else
      {
       printf("It is not an Identity Matrix\n");
      }
 }
 
 else
 {
  printf("Identity matrix should be a Square matrix \n");
 }
}

Explanation:

//Coming Soon...

Output:

After 1st Run:

To find whether given Matrix is Identity or not


After 2nd Run:

To find whether given Matrix is Identity or not



After 3rd Run:

To find whether given Matrix is Identity or not




 
Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide