# C++ Program to Check if a Matrix is a Square Matrix

«
»

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

Problem Description

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.

Problem Solution

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.

C++ Program/Source code

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.

1. `#include<iostream>`
2. `using namespace std;`
3. `int main ()`
4. `{`
5. `    int m, n, i, j, A;`
6. `    cout << "Enter the number of rows and columns of the matrix : ";`
7. `    cin >> m >> n;`
8. `    cout << "Enter the array elements : ";`
9. `    for (i = 0; i < m; i++)`
10. `        for (j = 0; j < n; j++)`
11. `            cin >> A[i][j];`
12. `    cout << "Matrix : \n ";`
13. `    for (i = 0; i < m; i++)`
14. `    {`
15. `        for (j = 0; j < n; j++)`
16. `            cout << A[i][j] << "  ";`
17. `        cout << "\n ";`
18. `    }`
19. `    if (m == n)`
20. `        cout << "The entered array is a square matrix.";`
21. `    else`
22. `    {`
23. `        cout << "The entered array is not a square matrix.";	`
24. `        exit(0);`
25. `    }`
26. `    cout << "The diagonal elements are : \n";`
27. `    for (i = 0; i < m; i++)`
28. `    {`
29. `        for (j = 0; j < n; j++)`
30. `        {`
31. `            if (i == j)`
32. `                cout << A[i][j];`
33. `            else`
34. `                cout << "  ";`
35. `        }`
36. `        cout << "\n";`
37. `    }`
38. `    return 0;`
39. `}`
Program Explanation

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.

Runtime Test Cases
```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. 