Table of Contents
The program receives a \Large 3 \times 3 matrix and computes the determinant and prints the results. The user provides the values for the matrix.
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.
Visit our Linear algebra hub page to learn these concepts.
Problem Definition
The determinant of a \Large 3 \times 3 matrix is easy to calculate. Before we dive into the code, let’s understand the procedure to calculate the determinant of a \Large 3 \times 3 matrix.
Suppose you are given a \Large 3 \times 3 matrix like one given below.

The next step is to find the co-factor of \Large A_{11}, A_{21} and \Large A_{31}.
\Large \begin{aligned}& 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)
\end{aligned}Now the value of determinant for \Large 3 \times 3 matrix is
\Large \begin{aligned}&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)\end{aligned}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[3][3],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[0][0] * ((a[1][1] * a[2][2])
- (a[2][1] * a[1][2]));
r2 = a[0][1] * ((a[1][0] * a[2][2])
- (a[2][0] * a[1][2]));
r3 = a[0][2] * ((a[1][0] * a[2][1])
- (a[2][0] * a[1][1]));
detval= r1 - r2 + r3;
printf("Determinant=%d\n",detval);
system("PAUSE");
return 0;
}#include <iostream>
using namespace std;
int main() {
int a[3][3], i, j;
int r1, r2, r3, detval;
cout << "Enter Elements of Matrix Row wise:\n";
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
cin >> a[i][j];
r1 = a[0][0] * ((a[1][1] * a[2][2]) - (a[2][1] * a[1][2]));
r2 = a[0][1] * ((a[1][0] * a[2][2]) - (a[2][0] * a[1][2]));
r3 = a[0][2] * ((a[1][0] * a[2][1]) - (a[2][0] * a[1][1]));
detval = r1 - r2 + r3;
cout << "Determinant = " << detval << endl;
return 0;
}import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] a = new int[3][3];
int r1, r2, r3, detval;
System.out.println("Enter Elements of Matrix Row wise:");
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
a[i][j] = sc.nextInt();
r1 = a[0][0] * ((a[1][1] * a[2][2]) - (a[2][1] * a[1][2]));
r2 = a[0][1] * ((a[1][0] * a[2][2]) - (a[2][0] * a[1][2]));
r3 = a[0][2] * ((a[1][0] * a[2][1]) - (a[2][0] * a[1][1]));
detval = r1 - r2 + r3;
System.out.println("Determinant = " + detval);
sc.close();
}
}a = [[0]*3 for _ in range(3)]
print("Enter Elements of Matrix Row wise:")
for i in range(3):
for j in range(3):
a[i][j] = int(input())
r1 = a[0][0] * ((a[1][1] * a[2][2]) - (a[2][1] * a[1][2]))
r2 = a[0][1] * ((a[1][0] * a[2][2]) - (a[2][0] * a[1][2]))
r3 = a[0][2] * ((a[1][0] * a[2][1]) - (a[2][0] * a[1][1]))
detval = r1 - r2 + r3
print("Determinant =", detval)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 \Large -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 . . . _