The Program in C to obtain the determinant value of a Matrix is given below:
#include <stdio.h>
#include <math.h>
// Function to find the determinant of a square matrix
double determinant(double a[10][10], int n)
{
double det = 0;
int i, j, k, subi, subj;
double submat[10][10];
if (n == 2)
{
return ((a[0][0] * a[1][1]) - (a[1][0] * a[0][1]));
}
else
{
for (k = 0; k < n; k++)
{
subi = 0;
for (i = 1; i < n; i++)
{
subj = 0;
for (j = 0; j < n; j++)
{
if (j == k)
{
continue;
}
submat[subi][subj] = a[i][j];
subj++;
}
subi++;
}
det = det + (pow(-1, k) * a[0][k] * determinant(submat, n - 1));
}
return det;
}
}
int main()
{
double a[10][10], det;
int i, j, n;
// Input the size of the matrix
printf("Enter the size of matrix: ");
scanf("%d", &n);
// Input the elements of the matrix
printf("Enter the elements of the matrix: \n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%lf", &a[i][j]);
}
}
// Determine the determinant of the matrix
det = determinant(a, n);
// Print the result
printf("Determinant of the matrix: %lf", det);
return 0;
}
Output:
Enter the size of matrix: 2
Enter the elements of the matrix:
1
4
6
7
Determinant of the matrix: -17.000000
Pro-Tips💡
The main difference is that I included the <math.h> library, which is needed for the pow() function used to calculate the determinant.
This program uses a recursive function determinant()
to calculate the determinant of the square matrix.
The program first takes the size of the matrix as input and then takes the elements of the matrix as input.
Finally, it calls the determinant()
function to calculate the determinant of the matrix and prints the result.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.