# C Program to Compute Determinant of a 3 x 3 Matrix

The program receives a 3 x 3 matrix and computes the determinant and prints the results. The user provides the values for the matrix.

We compiled the program using Dev-C++ 5.0 compiler, but you can use a different compiler such as Turbo C++ 3.0.

You must be familiar with the concept of the matrix and its determinant to understand this example. See the problem definition section for a brief introduction.

Learn following C programming concepts before you learn the example.

## Problem Definition

The determinant of a 3 x3 matrix is easy to calculate. Before we dive into the code, let’s understand the procedure to calculate the determinant of a 3 x 3 matrix.

Suppose you are given a 3 x3 matrix like one given below.

The next step is to find the co-factor of A11, A21 and A31. $A_{11} \hspace{1ex} = \hspace{1ex} a_1 (b_2 \hspace{1ex} \cdot \hspace{1ex} c_3 \hspace{1ex} - c_2 \hspace{1ex} \cdot b_3 )$ $A_{21} \hspace{1ex} = \hspace{1ex} -a_2(b_1 \hspace{1ex} \cdot \hspace{1ex} c_3 \hspace{1ex} - c_1 \hspace{1ex} \cdot b_3)$ $A_{31} \hspace{1ex} = \hspace{1ex} a_3(b_1 \hspace{1ex} \cdot \hspace{1ex} c_2 \hspace{1ex} - c_1 \hspace{1ex} \cdot \hspace{1ex} b_2)$

Now the value of determinant for 3×3 matrix is $Determinant \hspace{1ex} = \hspace{1ex} a_1(b_2 \hspace{1ex} \cdot \hspace{1ex} c_3 \hspace{1ex} - \hspace{1ex} c_2 \hspace{1ex} \cdot \hspace{1ex} b_3) \hspace{1ex} \\ \\ \hspace{1ex}- a_2(b_1 \hspace{1ex} \cdot \hspace{1ex} c_3 \hspace{1ex} - \hspace{1ex} c_1 \hspace{1ex} \cdot \hspace{1ex} b_3) \\ \\ \hspace{1ex} + \hspace{1ex} a_3( b_1 \hspace{1ex} \cdot \hspace{1ex} c_2 \hspace{1ex} -\hspace{1ex} c_1 \hspace{1ex} \cdot \hspace{1ex} b_2)$

## Program Code – Determinant of 3 x 3 Matrix

/*Program to compute the determinant of a matrix */

#include <stdio.h>

#include <stdlib.h>

int main()
{

int a,i,j,m,n,detval;

int r1,r2,r3;

r1 = 0;

r2 = 0;

r3 = 0;

detval = 0;

printf("Enter Elements of Matrix Row wise:\n");

for(i=0;i<3;i++)
{

for(j=0;j<3;j++)
{

scanf("%d",&a[i][j]);

}

}

r1 = a * ((a * a)
- (a * a));

r2 = a * ((a * a)
- (a * a));

r3 = a * ((a * a)
- (a * a));

detval= r1 - r2 + r3;

printf("Determinant=%d\n",detval);

system("PAUSE");

return 0;

}

## Output

When the program executes, it ask for input values for the matrix. You must enter the values row-wise. Upon entering the values, it computes the determinant and prints the output to the console.

You can enter negative values too, the compiler will consider that as an negative integer value.The output value as you can see is a negative integer value of -11.

You can verify the answer by doing the math on a paper and compare the results with program output. Try solving for different values for matrix.

Enter Elements of Matrix Row Wise:
1 4 5
1 3 1
2 3 1
Determinant=-11
Press any key to continue . . . _