# C++ Program to Check if a Matrix is Orthogonal or Not

«
»

This is a C++ Program to Find if a Matrix is Orthogonal.

Problem Description

The program takes a matrix and checks if it is orthogonal. A square matrix is defined as an orthogonal matrix if its transpose matrix is same as its inverse matrix.

Problem Solution

1. The program takes a square matrix.
2. The transpose of the matrix is found.
3. The product of the matrix and transpose are calculated and stored.
4. If the product is an identity matrix, then it is orthogonal.
5. Else it is not an orthogonal matrix.
6. The result is printed.
7. Exit.

C++ Program/Source code

Here is the source code of C++ Program to Find if a Matrix is Orthogonal. The program output is shown below.

1. `#include<iostream>`
2. `using namespace std;`
3. `int main ()`
4. `{`
5. `    int m, n, p, i, j, k;`
6. `    int A[10][10], T[10][10], P[10][10];`
7. `    cout << "Enter number of rows and columns : ";`
8. `    cin >> m >> n;`
9. `    if (m != n)`
10. `    {`
11. `        cout << "Matrix is not a square matrix!";`
12. `        exit(0);`
13. `    }`
14. `    cout << "Enter elements of matrix : ";`
15. `    for (i = 0; i < m; i++)`
16. `        for (j = 0; j < n; j++)`
17. `            cin >> A[i][j];`
18. ` `
19. `    for (i = 0; i < m; i++)`
20. `        for (j = 0; j < n; j++)`
21. `            T[j][i] = A[i][j];`
22. ` `
23. `    for (i = 0; i < m; i++)`
24. `    {`
25. `        for (j = 0; j < n; j++)`
26. `        { `
27. `            int sum=0;`
28. `            for ( k = 0 ; k < n ; k++ )`
29. `                sum += A[i][k] * T[k][j];`
30. `            P[i][j] = sum;`
31. `        }			`
32. `    }`
33. `    for (i = 0; i < m; i++)`
34. `    {`
35. `        for (j = 0; j < n; j++)`
36. `        {`
37. `            if (i == j)`
38. `            {`
39. `                if (P[i][j] != 1 )`
40. `                    break;`
41. `            }`
42. `            else`
43. `            {`
44. `                if (P[i][j] != 0 )`
45. `                    break;`
46. `            }`
47. `        }`
48. `        if (j != m)`
49. `            break;`
50. `    }`
51. `    if (i != m)`
52. `        cout << "Matrix is not orthogonal.\n ";`
53. `    else`
54. `        cout << "Matrix is orthogonal\nPrinting Original Matrix:\n ";`
55. `	for (i = 0; i < m; i++)`
56. `    {`
57. `        for (j = 0; j < n; j++)	`
58. `            cout << A[i][j] << " ";`
59. `        cout << "\n ";`
60. `    }`
61. `    return 0;`
62. `}`
Program Explanation

1. The user is asked to enter the number of rows and columns of the matrix. If they are not equal, program is exited.
2. The elements of the matrix are asked to enter and stored in ‘A’.
3. Transpose of the matrix is stored in the array ‘T’.
4. The product of the matrix and its transpose is stored in the matrix ‘P’.
5. If P is an identity matrix, then the matrix is orthogonal, else not.
6. The result is then printed.

Note: Join free Sanfoundry classes at Telegram or Youtube
Runtime Test Cases
```Case 1 :
Enter number of rows and columns : 2 2
Enter elements of matrix : 0 1 1 0
Matrix is orthogonal.
0 1
1 0

Case 2 :
Enter number of rows and columns : 3 3
Enter elements of matrix : -2 1 1 1 -2 1 1 1 -2
Matrix is not orthogonal.
-2 1 1
1 -2 1
1 1 -2

Case 3 :
Enter number of rows and columns : 3 3
Enter elements of matrix : 1 0 0 0 1 0 0 0 1
Matrix is orthogonal.
1 0 0
0 1 0
0 0 1```

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

To practice all C++ programs, here is complete set of 1000+ C++ Programming examples.

Take C++ Programming Mock Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10