This is a C++ Program to Find if an Array is a Square Matrix and Print the Diagonals.

The program takes an array and checks if it is a square matrix and prints the diagonals. A square matrix is one which has equal number of row and columns.

1. The program initially takes the number of rows and columns and stores the array.

2. If the number of rows and columns are equal, then the entered array is a square matrix, else not.

3. If it is a square matrix, the diagonals are printed.

4. Exit.

Here is the source code of C++ Program to Find if an Array is a Square Matrix and Print the Diagonals. The program output is shown below.

`#include<iostream>`

using namespace std;

int main ()

`{`

int m, n, i, j, A[10][10];

cout << "Enter the number of rows and columns of the matrix : ";

cin >> m >> n;

cout << "Enter the array elements : ";

for (i = 0; i < m; i++)

for (j = 0; j < n; j++)

cin >> A[i][j];

cout << "Matrix : \n ";

for (i = 0; i < m; i++)

`{`

for (j = 0; j < n; j++)

cout << A[i][j] << " ";

cout << "\n ";

`}`

if (m == n)

cout << "The entered array is a square matrix.";

`else`

`{`

cout << "The entered array is not a square matrix.";

exit(0);

`}`

cout << "The diagonal elements are : \n";

for (i = 0; i < m; i++)

`{`

for (j = 0; j < n; j++)

`{`

if (i == j)

cout << A[i][j];

`else`

cout << " ";

`}`

cout << "\n";

`}`

return 0;

`}`

1. The user is asked to enter the rows and columns and stored in the variables ‘i’ and ‘j’.

2. The array is also asked to enter and stored in the variable ‘A’.

3. The array is then printed accordingly.

4. If i is equal to j, then the array is a square matrix and the diagonals are printed.

5. Else the entered array is not a square matrix and the program is exited.

6. The result is then printed.

Case 1 : Enter the number of rows and columns of the : 3 3 Enter the array elements : 1 2 3 4 5 6 7 8 9 Matrix : 1 2 3 4 5 6 7 8 9 The entered array is a square matrix. The diagonal elements are 1 5 9 Case 2 : Enter the number of rows and columns of the matrix : 3 2 Enter the array elements : 10 20 30 40 50 60 Matrix : 10 20 30 40 50 60 The entered array is not a square matrix. Case 3 : Enter the number of rows and columns of the matrix : 2 2 Enter the array elements : 4 3 2 1 Matrix : 4 3 2 1 The entered array is a square matrix. The diagonal elements are : 4 1

**Sanfoundry Global Education & Learning Series – C++ Programs.**

To practice all C++ programs, __here is complete set of 1000+ C++ Programming examples__.