C Program to Find the Determinant of a Matrix

Introduction

The determinant of a matrix is a scalar value that can be computed from the elements of a square matrix. The determinant is a useful property in linear algebra and can be used to solve systems of linear equations, among other applications. This guide will show you how to write a C program to find the determinant of a 2×2 or 3×3 matrix.

Problem Statement

Create a C program that:

  • Takes a square matrix (either 2×2 or 3×3) as input from the user.
  • Calculates the determinant of the matrix.
  • Displays the determinant.

Example:

  • Input:

    • Matrix (2×2):
      1 2
      3 4
      
  • Output: Determinant = -2

  • Input:

    • Matrix (3×3):
      6 1 1
      4 -2 5
      2 8 7
      
  • Output: Determinant = -306

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Write the Main Function: Define the main function, which is the entry point of every C program.
  3. Declare Variables: Declare variables to store the matrix and the determinant.
  4. Input the Elements of the Matrix: Use loops to take input from the user for the matrix.
  5. Calculate the Determinant:
  • For a 2×2 matrix, use the formula: det = (a11 * a22) - (a12 * a21).
  • For a 3×3 matrix, use the formula: det = a11(a22*a33 - a23*a32) - a12(a21*a33 - a23*a31) + a13(a21*a32 - a22*a31).
  1. Display the Determinant: Use printf to display the determinant.

C Program to Find the Determinant of a 2×2 Matrix

#include <stdio.h>

int main() {
    // Step 1: Declare variables to hold the matrix and determinant
    int matrix[2][2];
    int determinant;

    // Step 2: Input the elements of the matrix
    printf("Enter elements of the 2x2 matrix:\n");
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    // Step 3: Calculate the determinant using the formula for a 2x2 matrix
    determinant = (matrix[0][0] * matrix[1][1]) - (matrix[0][1] * matrix[1][0]);

    // Step 4: Display the determinant
    printf("Determinant of the 2x2 matrix is: %d\n", determinant);

    return 0;  // Step 5: Return 0 to indicate successful execution
}

C Program to Find the Determinant of a 3×3 Matrix

#include <stdio.h>

int main() {
    // Step 1: Declare variables to hold the matrix and determinant
    int matrix[3][3];
    int determinant;

    // Step 2: Input the elements of the matrix
    printf("Enter elements of the 3x3 matrix:\n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    // Step 3: Calculate the determinant using the formula for a 3x3 matrix
    determinant = matrix[0][0] * (matrix[1][1] * matrix[2][2] - matrix[1][2] * matrix[2][1])
                - matrix[0][1] * (matrix[1][0] * matrix[2][2] - matrix[1][2] * matrix[2][0])
                + matrix[0][2] * (matrix[1][0] * matrix[2][1] - matrix[1][1] * matrix[2][0]);

    // Step 4: Display the determinant
    printf("Determinant of the 3x3 matrix is: %d\n", determinant);

    return 0;  // Step 5: Return 0 to indicate successful execution
}

Explanation

Step 1: Declare Variables

  • The matrix array stores the elements of the matrix. The variable determinant is used to store the calculated determinant.

Step 2: Input the Elements of the Matrix

  • The program uses nested for loops to take input for each element of the matrix from the user.

Step 3: Calculate the Determinant

  • For a 2×2 matrix, the determinant is calculated using the formula:
    [
    \text{det} = (a_{11} \times a_{22}) – (a_{12} \times a_{21})
    ]
  • For a 3×3 matrix, the determinant is calculated using the formula:
    [
    \text{det} = a_{11}(a_{22} \times a_{33} – a_{23} \times a_{32}) – a_{12}(a_{21} \times a_{33} – a_{23} \times a_{31}) + a_{13}(a_{21} \times a_{32} – a_{22} \times a_{31})
    ]

Step 4: Display the Determinant

  • The program uses the printf function to display the calculated determinant.

Step 5: Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example 1 (2×2 Matrix):

Enter elements of the 2x2 matrix:
1 2
3 4
Determinant of the 2x2 matrix is: -2

Example 2 (3×3 Matrix):

Enter elements of the 3x3 matrix:
6 1 1
4 -2 5
2 8 7
Determinant of the 3x3 matrix is: -306

Conclusion

This C program demonstrates how to calculate the determinant of a 2×2 or 3×3 matrix. It covers basic concepts such as arrays, loops, and matrix operations, making it a useful example for beginners learning C programming.

Leave a Comment

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

Scroll to Top