C program to find determinant of Matrix | C code champ

Mr Coder August 22, 2012 10




C program to find determinant of Matrix : The determinant of a matrix is a number that can be calculated from the matrix. It tells us things about the matrix that are useful in systems of linear equations, in calculus and more. First of all, if you want to calculate the determinant of matrix then your matrix should be an square matrix i.e. MxM , otherwise determinant will not exist.

If you wish to calculate determinant of 3×3 matrix then formula will be something like below :

Say 3×3 matrix is :

Sample 3x3 Matrix

Sample 3×3 Matrix

Then its determinant will be like below:

Determinant of 3x3 matrix

Determinant of 3×3 matrix

Note : |  |  denotes determinant . That means 3 x 3 matrix has been broken into 3 small determinants

Now consider an example of 4×4 matrix :

Determinant of 4x4 Matrix

Determinant of 4×4 Matrix

Notice the + – + – pattern (+a… -b… +c… -d…). This is important to remember.

The pattern continues for 5×5 matrices and higher.

Now let us see how to write a C program to find determinant of Matrix MxM . The program will accept the order of matrix i.e. M as input from user and then ask user to enter the values of matrix elements. Then it will calculate the determinant of the matrix and returns the output.

C program to find determinant of Matrix :

#include<conio.h>
#include<stdio.h>

int a[20][20],m;
int determinant(int f[20][20],int a);
int main()
{
  int i,j;
  printf("----------------------------------------------------------------------\n");
  printf("-------------------made by C code champ ------------------------------\n");
  printf("----------------------------------------------------------------------\n");
  printf("\n\nEnter order of matrix : ");
  scanf("%d",&m);
  printf("\nEnter the elements of matrix\n");
  for(i=1;i<=m;i++)
  {
  for(j=1;j<=m;j++)
  {
  printf("a[%d][%d] = ",i,j);
  scanf("%d",&a[i][j]);
  }
  }
  printf("\n\n---------- Matrix A is --------------\n");    
  for(i=1;i<=m;i++)
     {
          printf("\n");
          for(j=1;j<=m;j++)
          {     
               printf("\t%d \t",a[i][j]);
          }
     }
  printf("\n \n");
  printf("\n Determinant of Matrix A is %d .",determinant(a,m));
  getch();
}

int determinant(int f[20][20],int x)
{
  int pr,c[20],d=0,b[20][20],j,p,q,t;
  if(x==2)
  {
    d=0;
    d=(f[1][1]*f[2][2])-(f[1][2]*f[2][1]);
    return(d);
   }
  else
  {
    for(j=1;j<=x;j++)
    {        
      int r=1,s=1;
      for(p=1;p<=x;p++)
        {
          for(q=1;q<=x;q++)
            {
              if(p!=1&&q!=j)
              {
                b[r][s]=f[p][q];
                s++;
                if(s>x-1)
                 {
                   r++;
                   s=1;
                  }
               }
             }
         }
     for(t=1,pr=1;t<=(1+j);t++)
     pr=(-1)*pr;
     c[j]=pr*determinant(b,x-1);
     }
     for(j=1,d=0;j<=x;j++)
     {
       d=d+(f[1][j]*c[j]);
      }
     return(d);
   }
}

We hope you all have enjoyed the C program to find determinant of Matrix. If you have any issues with the above code or logic of above code then ask us in form of comment.

10 Comments »

  1. Nidhin September 17, 2012 at 3:20 pm - Reply

    Very simple program but powerfull.Thanks pal……….

  2. wow gold October 10, 2012 at 10:56 pm - Reply

    Itˇs really a nice and helpful piece of info. I am glad that you just shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.

  3. Reda Brackin November 6, 2012 at 7:25 pm - Reply

    You are so gifted! Count your self fortunate

  4. sara December 3, 2012 at 9:27 pm - Reply

    slt je veux un programme pour calculer det(A)=0, A est une matrice, a un variable . je veux calculer det(A)=0 pour trouver ce variable (je vais tomber sur une polynome de degré 6, et je dois calculer les racines de cette polynome). merci d’avance

  5. sara December 4, 2012 at 10:07 am - Reply

    i want program to calcul the “valeur propre” d’une matrice 4*4 en utilisant polynome caracteristique

  6. Adeolu January 18, 2013 at 5:52 am - Reply

    pls i dont knw if you can help write dis same code in c++ bcos am having problems writing it in c++.thanks

  7. Crea Torr March 9, 2013 at 9:12 pm - Reply

    Can u plz tll me “What is the use of t ,and incrementing it if it not further used in program?”

  8. sarath July 6, 2014 at 8:57 am - Reply

    Happy to know that u are on our generatiom
    all the best & attain peaks…
    – visitor™

  9. Jalaj Negi September 8, 2014 at 4:12 pm - Reply

    /* Little improvement 2 above function – 10×10 is taken instead of 20×20 */

    int determinant(int f[10][10], int x)
    {
    int b[10][10],c[10], i,j,k,s, sign =1, det = 0;

    if(2==x)
    {
    det = f[0][0]*f[1][1] – f[0][1]*f[1][0];
    }
    else
    {
    for (i=0;i<x;i++,sign*=-1)
    {
    for(j=1;j<x;j++)
    {
    for(s=k=0;k<x;k++)
    {
    if(i != k)
    {
    b[j-1][s] = f[j][k];
    s++;
    }
    }
    }

    det += (sign * f[0][i] * determinant(b,x-1));
    }
    }

    return det;
    }

  10. xyz September 11, 2014 at 7:28 am - Reply

    showing segmentation error wen i pass input matrix of order 100 at line
    b[r][s]=f[p][q];

Leave A Response »