C and C++ Program to Find Inverse of a Matrix

Here you will get a C and C++ program to discover backwards of a network.

We can acquire grid reverse by the following technique.

First figure determinant of the lattice.

At that point figure adjoint of the given lattice. Adjoint can be gotten by taking the transpose of the cofactor network of a given square lattice.

At last duplicate 1/deteminant by adjoint to get reverse.

The recipe to discover the opposite of network is given underneath.

Inverse of a Matrix

Watch This Video To Learn More

C Program to Find Inverse of a Matrix

#include<stdio.h>
 
int main(){
	int mat[3][3], i, j;
	float determinant = 0;
	
	printf("Enter elements of matrix row wise:\n");
	for(i = 0; i < 3; i++)
		for(j = 0; j < 3; j++)
           scanf("%d", &mat[i][j]);
	
	printf("\nGiven matrix is:");
	for(i = 0; i < 3; i++){
		printf("\n");
		
		for(j = 0; j < 3; j++)
			printf("%d\t", mat[i][j]);
	}
	
	//finding determinant
	for(i = 0; i < 3; i++)
		determinant = determinant + (mat[0][i] * (mat[1][(i+1)%3] * mat[2][(i+2)%3] - mat[1][(i+2)%3] * mat[2][(i+1)%3]));
	
	printf("\n\ndeterminant: %f\n", determinant);
	
	printf("\nInverse of matrix is: \n");
	for(i = 0; i < 3; i++){
		for(j = 0; j < 3; j++)
			printf("%.2f\t",((mat[(j+1)%3][(i+1)%3] * mat[(j+2)%3][(i+2)%3]) - (mat[(j+1)%3][(i+2)%3] * mat[(j+2)%3][(i+1)%3]))/ determinant);
		
		printf("\n");
	}
 
   return 0;
}

C++ Program to Find Inverse of a Matrix

#include<iostream>
 
using namespace std;
 
int main(){
	int mat[3][3], i, j;
	float determinant = 0;
	
	cout<<"Enter elements of matrix row wise:\n";
	for(i = 0; i < 3; i++)
		for(j = 0; j < 3; j++)
           cin>>mat[i][j];
	
	printf("\nGiven matrix is:");
	for(i = 0; i < 3; i++){
		cout<<"\n";
		
		for(j = 0; j < 3; j++)
			cout<<mat[i][j]<<"\t";
	}
	
	//finding determinant
	for(i = 0; i < 3; i++)
		determinant = determinant + (mat[0][i] * (mat[1][(i+1)%3] * mat[2][(i+2)%3] - mat[1][(i+2)%3] * mat[2][(i+1)%3]));
	
	cout<<"\n\ndeterminant: "<<determinant;
	
	cout<<"\n\nInverse of matrix is: \n";
	for(i = 0; i < 3; i++){
		for(j = 0; j < 3; j++)
			cout<<((mat[(j+1)%3][(i+1)%3] * mat[(j+2)%3][(i+2)%3]) - (mat[(j+1)%3][(i+2)%3] * mat[(j+2)%3][(i+1)%3]))/ determinant<<"\t";
		
		cout<<"\n";
	}
 
   return 0;
}

Output

Leave a Comment

error: Alert: Content is protected!!