w3resource

C Exercises: Calculate the determinant of a 3 x 3 matrix

C Array: Exercise-28 with Solution

Write a program in C to calculate determinant of a 3 x 3 matrix.

Pictorial Presentation:

C Exercises: Calculate the determinant of a 3 x 3 matrix

Sample Solution:

C Code:

#include <stdio.h>

void main()
   {
  int arr1[10][10],i,j,n;
  int det=0;
  
  
       printf("\n\nCalculate the determinant of a 3 x 3 matrix :\n");
       printf("-------------------------------------------------\n");  

	 printf("Input elements in the first matrix :\n");
       for(i=0;i<3;i++)
        {
            for(j=0;j<3;j++)
            {
	           printf("element - [%d],[%d] : ",i,j);
	           scanf("%d",&arr1[i][j]);
            }
        }  
	 printf("The matrix is :\n");
	 for(i=0;i<3;i++)
	 {
	   for(j=0;j<3 ;j++)
	     printf("% 4d",arr1[i][j]);
	    printf("\n");
	 }

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

  printf("\nThe Determinant of the matrix is: %d\n\n",det);
}

Sample Output:

Calculate the determinant of a 3 x 3 matrix :                                                                 
-------------------------------------------------                                                             
Input elements in the first matrix :                                                                          
element - [0],[0] : 1                                                                                         
element - [0],[1] : 0                                                                                         
element - [0],[2] : -1                                                                                        
element - [1],[0] : 0                                                                                         
element - [1],[1] : 0                                                                                         
element - [1],[2] : 1                                                                                         
element - [2],[0] : -1                                                                                        
element - [2],[1] : -1                                                                                        
element - [2],[2] : 0                                                                                         
The matrix is :                                                                                               
   1   0  -1                                                                                                  
   0   0   1                                                                                                  
  -1  -1   0                                                                                                  
                                                                                                              
The Determinant of the matrix is: 1 

Flowchart:

Flowchart: Calculate the determinant of a 3 x 3 matrix.

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to print or display upper triangular matrix.
Next: Write a program in C to accept a matrix and determine whether it is a sparse matrix.

What is the difficulty level of this exercise?



Inviting useful, relevant, well-written and unique guest posts