Friday 10 November 2017

C Program to Check if a given Matrix is an Identity Matrix


Code

#include

void main()
{
    int a[10][10];
    int i, j, row, column, flag = 1;

    printf("Enter the order of the matrix A \n");
    scanf("%d %d", &row, &column);
    printf("Enter the elements of matrix A \n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
    printf("MATRIX A is \n");
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            printf("%3d", a[i][j]);
        }
        printf("\n");
    }
    /*  Check for unit (or identity) matrix */
    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            if (a[i][j] != 1 && a[j][i] != 0)
            {
                flag = 0;
                break;
            }
        }
    }
    if (flag == 1 )
        printf("It is identity matrix \n");
    else
        printf("It is not a identity matrix \n");
}

Output

Enter the order of the matrix A
3 3
Enter the elements of matrix A
1 2 3
5 1 8
6 4 1
MATRIX A is
  1  2  3
  5  1  8
  6  4  1
It is not a identity matrix

$ a.out
Enter the order of the matrix A
3 3
Enter the elements of matrix A
 1 0 0
 0 1 0
 0 0 1
MATRIX A is
  1  0  0
  0  1  0
  0  0  1
It is identity matrix

More C Program












100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...