C program to find determinant of a matrix






Code to find determinant of a matrix

C program to calculate determinant of a matrix



#include<stdio.h>
int main(){
  int a[3][3],i,j;
  int determinant=0;

  printf("Enter the 9 elements of matrix: ");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);

  printf("\nThe First matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           printf("%d\t",a[i][j]);
  }

  for(i=0;i<3;i++)
      determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));

  printf("\nDeterminant of matrix is: %d",determinant);

   return 0;
}


C code for Determinant of 2X2 matrix:

#include<stdio.h>
int main(){
  int a[2][2],i,j;
  long determinant;

  printf("Enter the 4 elements of matrix: ");
  for(i=0;i<2;i++)
      for(j=0;j<2;j++)
           scanf("%d",&a[i][j]);

  printf("\nThe matrix is\n");
  for(i=0;i<2;i++){
      printf("\n");
      for(j=0;j<2;j++)
           printf("%d\t",a[i][j]);
  }

  determinant = a[0][0]*a[1][1] - a[1][0]*a[0][1];

  printf("\nDeterminant of 2X2 matrix: %ld",determinant);
 
   return 0;
}

Enter the 4 elements of matrix: 4
8
3
9

The matrix is

4       8
3       9

Determinant of 2X2 matrix: 12

C code for Determinant of 3X3 matrix:

#include<stdio.h>

int main(){

  int a[3][3],i,j;

  long determinant;



  printf("Enter the 9 elements of matrix: ");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);

  printf("\nThe matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           printf("%d\t",a[i][j]);
  }

  determinant = a[0][0]*((a[1][1]*a[2][2]) - (a[2][1]*a[1][2])) -a[0][1]*(a[1][0]*a[2][2] - a[2][0]*a[1][2]) + a[0][2]*(a[1][0]*a[2][1] - a[2][0]*a[1][1]);

  printf("\nDeterminant of 3X3 matrix: %ld",determinant);
 
   return 0;
}

Sample output:
Enter the 9 elements of matrix: 1
2
3
4
5
6
7
8
9

The matrix is

1       2       3
4       5       6
7       8       9

Determinant of 3X3 matrix: 0








Alogrithm:

Determinant is possible only for square matrixes i.e. n by n matrixes.
Determinant of 2 by 2 matrix:









Determinant of matrix has defined as: ad – cb

Determinant of 3 by 3 matrix:

Determinant of matrix has defined as: a00(a11*a22 – a21*a12) + a01(a10*a22 – a20*a12) + a02(a10*a21 – a20*a11)















14 comments:

Anonymous said...

thanks....all the programs are very helpful....

Anonymous said...

Can i get a c program for rank of a matrix???

Anonymous said...

thank u...........

shanto said...

its helpful..

Anonymous said...

its very very help full thanks bro

Unknown said...

hi... very easy initiative taken....

but i have a doubt... wat is the usinf using a %3 in the first program of finding the determinant of 3x3 matrix??

determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));

Anonymous said...

Thank you very much

Anonymous said...

java program to find determinant of n*n matrix using recursion............
--and please call a instance of this class in main method...

import java.util.Random;
import java.util.Scanner;


public class Matrix {

int matrix[][];
Scanner s=new Scanner(System.in);
Random r = new Random();
public Matrix()
{
System.out.println("Enter size");
int n=s.nextInt();
int[][] matrix=new int[n][n];
System.out.println("enter the matrix");

for(int i=0;i<matrix.length;i++)
{
for(int j=0;j<matrix.length;j++)
{
matrix[i][j]=2+(r.nextInt(1000)%5);
}
}

for(int i=0;i<matrix.length;i++)
{
for(int j=0;j<matrix.length;j++)
{
System.out.print(matrix[i][j]+" ");
}
System.out.println();
}


// for(int i=0;i<matrix.length;i++)
// {
// for(int j=0;j<matrix.length;j++)
// {
// matrix[i][j]=s.nextInt();
// }
// }
//
long det=this.findDet(matrix);
System.out.println("determinant is "+det);

}



public long findDet(int matrix[][])
{

if(matrix.length==1)
return matrix[0][0];


if(matrix.length==2)
return (matrix[0][0]*matrix[1][1]-matrix[0][1]*matrix[1][0]);




long sum=0;
for(int j=0;j<matrix.length;j++)
{
int temp[][]=new int[matrix.length-1][matrix.length-1];

this.findNext(matrix, temp, j);

if(j%2==0)
sum=sum+matrix[0][j]*findDet(temp);
else
sum=sum-matrix[0][j]*findDet(temp);
}




return sum;
}


public void findNext(int higher[][],int lower[][],int colNo)
{
for(int i=1,k=0;i<higher.length;i++,k++)
{
for(int j=0,l=0;j<higher.length;l++,j++)
{
if(j!=colNo)
{
lower[k][l]=higher[i][j];

}
else{
l--;
}
}
}
}

}

Unknown said...

can u make 6 by 6 matrix using turbo c++ sir ! so needed

Abhay Pandey said...

determinant of N*N matrix:
public int determinant(int a[][], int n){
int det = 0, sign = 1, p = 0, q = 0;

if(n==1){
det = a[0][0];
}
else{
int b[][] = new int[n-1][n-1];
for(int x = 0 ; x < n ; x++){
p=0;q=0;
for(int i = 1;i < n; i++){
for(int j = 0; j < n;j++){
if(j != x){
b[p][q++] = a[i][j];
if(q % (n-1) == 0){
p++;
q=0;
}
}
}
}
det = det + a[0][x] *
determinant(b, n-1) *
sign;
sign = -sign;
}
}
return det;
}

Unknown said...

this program is not working

Jananie Balaji said...

the program for 3 by 3 matrix doesn't work because it is supposed to be -a[1][0] in the second time for loop execution.

Jananie Balaji said...

the program for 3 by 3 matrix doesn't work because it is supposed to be -a[1][0] in the second time for loop execution.

Jananie Balaji said...

the program for 3 by 3 matrix doesn't work because it is supposed to be -a[1][0] in the second time for loop execution.