To find the given matrix is a unit matrix
(or) not - 2
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int mat[10][10];
int i, j, size, flag = 1;
clrscr();
printf("Enter size of the square matrix : ");
scanf("%d", &size);
printf("\nEnter the elements of the matrix : \n\n");
for(i = 0; i < size; i++)
for(j = 0; j < size; j++)
scanf("%d", &mat[i][j]);
for(i = 0; i < size; i++)
{
for(j = 0; j < size; j++)
{
if(i == j)
if(mat[i][j] != 1)
flag = 0 ;
if(i != j)
if(mat[i][j] != 0)
flag = 0;
}
}
if(flag == 1)
printf("\nThe matrix is an unit matrix");
else
printf("\nThe matrix is not an unit matrix");
getch();
}
Output:
Case: 1
Enter size of the square matrix : 3
Enter the elements of the matrix :
1 0 0
0 1 0
0 0 1
The matrix is an unit matrix
Case: 2
Enter size of the square matrix : 3
Enter the elements of the matrix :
1 2 3
4 1 5
6 7 1
The matrix is not an unit matrix
-
UpdatedDec 31, 2019
-
Views8,702
You May Like
Program to maintain employee details using structures
Program to maintain student details using structures
Check whether the person is eligible to vote or not
Print the numbers that are divisible by a given no.
Program to generate magic square
To sort the given numbers in ascending & descending order