C Program to Check if a Matrix is an Identity Matrix

This is a program to check if a given matrix is an Identity Matrix

Problem Description

This C Program checks a given Matrix is an Identity Matrix. Identity matrix is a square matrix with 1’s along the diagonal from upper left to lower right and 0’s in all other positions. If it satisfies the structure as explained before then the matrix is called as identity matrix.

Problem Solution

1. Create a matrix (2D array) and define its elements according to its size.
2. Two check whether the matrix is an identity matrix or not, run a nested for loop with two iterators.
3. Using two iterators, locate each position of matrix and check whether the right diagonal have all 1s with remaining elements as 0s.
4. Set a flag variable 1 if it matches the above condition, otherwise it will be 0.
5. Print Identity matrix if flag is 1.

Program/Source Code

Here is source code of the C program to check a given Matrix is an Identity Matrix. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.  
  2. /*
  3. * C Program to check if a given matrix is an identity matrix
  4. */
  5. #include <stdio.h>
  6.  
  7. int main (void)
  8. {
  9. 	int a[10][10];
  10. 	int i = 0, j = 0, row = 0, col = 0;
  11.  
  12. 	printf ("Enter the order of the matrix (mxn):\n");
  13. 	printf ("where m = number of rows; and\n");
  14. 	printf ("      n = number of columns\n");
  15. 	scanf ("%d %d", &row, &col);
  16.  
  17. 	int flag = 0;
  18.  
  19. 	printf ("Enter the elements of the matrix\n");
  20. 	for (i = 0; i < row; i++)
  21. 	{
  22. 		for (j = 0; j < col; j++)
  23. 		{
  24. 			scanf ("%d", &a[i][j]);
  25. 		}
  26. 	}
  27.  
  28. 	for (i = 0; i < row; i++)
  29. 	{
  30. 		for (j = 0; j < col; j++)
  31. 		{
  32. 			if (i == j && a[i][j] != 1)
  33. 			{
  34. 				flag = -1;
  35. 				break;
  36. 			}
  37. 			else if (i != j && a[i][j] != 0)
  38. 			{
  39. 				flag = -1;
  40. 				break;
  41. 			}
  42. 		}
  43. 	}
  44.  
  45. 	if (flag == 0)
  46. 	{
  47. 		printf ("It is a IDENTITY MATRIX\n");
  48. 	}
  49. 	else
  50. 	{
  51. 		printf ("It is NOT an identity matrix\n");
  52. 	}
  53.  
  54. 	return 0;
  55. }
Program Explanation

1. Declare a 2D array (matrix), of some fixed capacity.
2. Take input from users the number of rows and columns. Accordingly define the elements of the array.
3. Now, run a nested for loop with 2 iterators, i and j which will help to locate each element of the array.
4. Inside the loop, create a condition which checks whether the element at right diagonal is 1 and remaining elements are 0s or not.
5. A variable flag is created and is set to 1 initially. This flag is set to 0 if it does not satisfy the above condition.
6. Appropriate message is printed according to the value of flag.

advertisement
Runtime Test Cases
 
Enter the order of the matrix (mxn):
where m = number of rows; and
      n = number of columns
3 3
Enter the elements of the matrix
1 0 0
0 1 0
0 0 1
It is a IDENTITY MATRIX
 
Enter the order of the matrix (mxn):
where m = number of rows; and
      n = number of columns
3 3
Enter the elements of the matrix
1 2 3
4 5 6
5 8 7
It is NOT an identity matrix

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

Free 30-Day Java Certification Bootcamp is Live. Join Now!

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.