# Adjoint of n x n Matrix in C++

Adjoint of a Matrix is the transpose of the co-factors of the given matrix. It is the core Mathematics concept used in calculating inverse of a matrix. In this Article, I am going to share with you the code of adjoint of n x n matrix in C++ where n=1,2 or 3.

## What is Adjoint of n x n Matrix

The adjoint of an n x n matrix is known as the adjugate or classical adjoint. It is a matrix that is related to the original matrix and is used in various mathematical operations, especially in finding the inverse of a matrix. The adjoint is denoted as “adj(A)” for a matrix A.

To find the adjoint of an n x n matrix A, you need to follow these steps:

1. Calculate the cofactor matrix of A.
• For each element A[i][j] of A, calculate the cofactor C[i][j]. The cofactor of an element is the determinant of the matrix obtained by removing the i-th row and j-th column from A, multiplied by (-1)^(i+j). In mathematical notation:
C[i][j] = (-1)^(i+j) * det(M[i][j]),
where M[i][j] is the (n-1) x (n-1) matrix obtained by removing the i-th row and j-th column from A.
1. Transpose the cofactor matrix.
• Swap the rows and columns of the cofactor matrix obtained in step 1 to get the adjoint matrix.

Mathematically, if C is the cofactor matrix of A, then the adjoint matrix adj(A) is given by:

The adjoint of a matrix plays a crucial role in finding the inverse of a matrix. Specifically, if A is an invertible (non-singular) matrix, then you can find the inverse of A as follows:
A^(-1) = (1 / det(A)) * adj(A)
where det(A) is the determinant of A.

Keep in mind that the adjoint is only defined for square matrices (n x n matrices), and it’s used primarily in linear algebra and matrix operations.

## Code in C++

``````//ADJOINT of matrices :)

#include<iostream>
#include<math.h>
#include<conio.h>
using namespace std;
int main()
{
int n;
system("color b4 ");
do
{
cout<<"\n\n\tEnter Size of Matrix : \n\n";
cout<<"\t1-One x One "<<endl;
cout<<"\t2-Two x Two"<<endl;
cout<<"\t3-Three x Three "<<endl<<"\t";
cin>>n;
}
while(n>4 || n<1) ;
//system("CLS");
//system("color F3");
cout<<"\n\n\tEnter Elements now \n";
int i,j;
//enter elements
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
cout<<"\tElement ["<<i+1<<"]["<<j+1<<"] : ";cin>>mat[i][j];
}
}
cout<<"\n\t";
if(n==1)
{

}
//2x2 matrix determinent
if(n==2)
{
//2X2 matrix determinent
}
if(n==3)
{
//cofactor 11
//cofactor 12
//cofactor 13
//cofactor 21
//cofactor 22
//cofactor 23
//cofactor 31
//cofactor 32
//cofactor 33
}
cout<<"\n\tADJOINT OF "<<n<<" x "<<n<<" MATRIX IS  \n\n";
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{