Q:

C Program to check identity matrix

0

C Program to check identity matrix

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>
#define SIZE 3 

int main()
{
    int A[SIZE][SIZE];
    int row, col, isIdentity;

   
    printf("Enter elements in matrix of size 3x3: \n");
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {
            scanf("%d", &A[row][col]);
        }
    }

   
    isIdentity = 1;
    for(row=0; row<SIZE; row++)
    {
        for(col=0; col<SIZE; col++)
        {

            if(row==col && A[row][col]!=1)
            {
               
                isIdentity = 0;
            }
            else if(row!=col && A[row][col]!=0)
            {
               
                isIdentity = 0;
            }
        }
    }

   
    if(isIdentity == 1)
    {
        printf("\nThe given matrix is an Identity Matrix.\n");

       
        for(row=0; row<SIZE; row++)
        {
            for(col=0; col<SIZE; col++)
            {
                printf("%d ", A[row][col]);
            }

            printf("\n");
        }
    }
    else
    {
        printf("The given matrix is not Identity Matrix");
    }

    return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now