C++ Program to find Matrix is an Identity Matrix

Write a C++ Program to find Matrix is an Identity Matrix with an example. A C++ Identity Matrix is a square matrix whose main diagonal items are 1’s, and all the other elements are zeros.

In this C++ Identity Matrix example, we allow the user to enter the rows and columns. Next, we used the C++ nested for loop to iterate the Matrix. Within the for loop, we used the C++ If statement to check whether the main diagonal items are ones and the non-diagonal items are zeros. If it is true, we changed the Flag value to 0, and apply the break statement to exit from the for loop. Next, we used the C++ If Else statement to print the identity matrix output based on the Flag value.

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, flag = 1;
	
	cout << "\nPlease Enter Matrix rows and Columns to find Identity Matrix =  ";
	cin >> i >> j;
	
	int identMatrix[i][j];
	
	cout << "\nPlease Enter the Identity Matrix Items\n";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> identMatrix[rows][columns];
		}		
	}

 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
    		if(identMatrix[rows][columns] != 1 && identMatrix[columns][rows] != 0)
    		{
    			flag = 0;
    			break;
			}
   	 	}
  	}
  	if(flag == 1)
  	{
  		cout << "\nThe Matrix that you entered is an Identity Matrix";
	}
	else
	{
		cout << "\nThe Matrix that you entered is Not an Identity Matrix";
	}  	

 	return 0;
}
C++ Program to find Matrix is an Identity Matrix 1

This  C++ code for identity matrix uses the Else If Statement to check whether the given matrix is an identity matrix. 

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, flag = 1;
	
	cout << "\nPlease Enter Matrix rows and Columns to find Identity Matrix =  ";
	cin >> i >> j;
	
	int identMatrix[i][j];
	
	cout << "\nPlease Enter the Identity Matrix Items\n";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> identMatrix[rows][columns];
		}		
	}

 	for(rows = 0; rows < i; rows++)
  	{
   		for(columns = 0; columns < j; columns++)
    	{
    		if(rows == columns && identMatrix[rows][columns] != 1)
    		{
    			flag = 0;
			}
			else if(rows != columns && identMatrix[rows][columns] != 0)
			{
				flag = 0;
			}
   	 	}
  	}
  	if(flag == 1)
  	{
  		cout << "\nThe Matrix that you entered is an Identity Matrix";
	}
	else
	{
		cout << "\nThe Matrix that you entered is Not an Identity Matrix";
	}  	

 	return 0;
}
Please Enter Matrix rows and Columns to find Identity Matrix =  3 3

Please Enter the Identity Matrix Items
1 0 0
0 1 0
0 0 1

The Matrix that you entered is an Identity Matrix

Let me try non identity matrix.

Please Enter Matrix rows and Columns to find Identity Matrix =  3 3

Please Enter the Identity Matrix Items
1 0 0
0 0 1
0 1 0

The Matrix that you entered is Not an Identity Matrix

C++ Program to find Matrix is an Identity Matrix using a While Loop

#include<iostream>
using namespace std;

int main()
{
	int i, j, rows, columns, flag = 1;
	
	cout << "\nPlease Enter Matrix rows and Columns to find Identity Matrix =  ";
	cin >> i >> j;
	
	int identMatrix[i][j];
	
	cout << "\nPlease Enter the Identity Matrix Items\n";
	for(rows = 0; rows < i; rows++)	{
		for(columns = 0; columns < i; columns++) {
			cin >> identMatrix[rows][columns];
		}		
	}
	
	rows = 0; 
 	while(rows < i )
  	{
  		columns = 0; 
   		while(columns < j)
    	{
    		if(rows == columns && identMatrix[rows][columns] != 1)
    		{
    			flag = 0;
			}
			else if(rows != columns && identMatrix[rows][columns] != 0)
			{
				flag = 0;
			}
			columns++;
   	 	}
   	 	rows++;
  	}
  	if(flag == 1)
  	{
  		cout << "\nThe Matrix that you entered is an Identity Matrix";
	}
	else
	{
		cout << "\nThe Matrix that you entered is Not an Identity Matrix";
	}  	

 	return 0;
}
Please Enter Matrix rows and Columns to find Identity Matrix =  3 3

Please Enter the Identity Matrix Items
1 0 0
0 1 0
0 0 1

The Matrix that you entered is an Identity Matrix