C Examples

WAP in C to obtain the determinant value of a Matrix

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.

Codeauri is Code Learning Hub and Community for every Coder to learn Coding by navigating Structurally from Basic Programming to Front-End Development, Back-End Development to Database, and many more.

Related Posts

C# Program to Find Sum of Rows & Columns of a Matrix

The Program in C# Program to Find Sum of Rows & Columns of a Matrix is given below: Output: Hello Codeauri Family,enter the number of rows and columns…

C# Program to Calculate Determinant of Given Matrix

The Program in C# Program to Calculate Determinant of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns of the matrix…

C# Program to Find Sum of right Diagonals of a Matrix

The Program in C# Program to Find Sum of right Diagonals of a Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns…

C# Program to Find Transpose of Given Matrix

The Program in C# Program to Find Transpose of Given Matrix is given below: Output: Hello Codeauri Family, enter the number of rows and columns in the matrix:22Enter…

C# Program for Multiplication of two square Matrices

The Program in C# Program for Multiplication of two square Matrices is given below: Output: Hello Codeauri Family, enter the number of rows/columns in the matrices:2Enter the elements…

C# Program to Delete Element at Desired position From Array

The Program in C# Program to Delete Element at Desired position From Array is given below: Output: Hello Codeauri Family, enter the number of elements in the array:4Enter…

Leave a Reply

Your email address will not be published. Required fields are marked *

Your Journey into Code Begins Now: Discover the Wonders of Basic Programming

X