Determinant of a matrix in C
In this post, you will learn how to find determinant of a 2x2 matrix and 3x3 matrix in C programming language.
The determinant of a matrix is a number that is specially defined only for square matrices. Determinants are mathematical objects that are very useful in the analysis and solution of systems of linear equations. The determinant is useful for solving linear equations, capturing how linear transformation change area or volume. Determinants also have wide applications in engineering, science, social science and economics as well.
The determinant of a matrix A is denoted det(A), det A, or |A|.
C Program to find the determinant of a Matrix 2x2
The given program allows the user to enter the rows and columns elements of a 2 * 2 Matrix. Then, we apply the determinant formula to calculate the given determinant of the matrix.
#include<stdio.h>
int main()
{
int rows, columns, m[2][2], determinant = 0;
printf("\n Please Enter the 2 * 2 Matrix Elements \n ");
for(rows = 0; rows < 2; rows++)
{
for(columns = 0;columns < 2; columns++)
{
scanf("%d", &m[rows][columns]);
}
}
determinant = (m[0][0] * m[1][1]) - (m[0][1] * m[1][0]);
printf("\n The Determinant of 2 * 2 Matrix = %d", determinant);
return 0;
}
Output of the above code:
Please Enter the 2 * 2 Matrix Elements
9 7
4 8
The Determinant of 2 * 2 Matrix = 44
C Program to find the determinant of a Matrix 3x3
The given program allows the user to enter the rows and columns elements of a 3 * 3 Matrix. Then, we apply the determinant formula to calculate the given determinant of the matrix.
#include<stdio.h>
int main()
{
int rows, columns, m[3][3];
int x, y, z, determinant = 0;
printf("\n Please Enter the 3 * 3 Matrix Elements \n");
for(rows = 0; rows < 3; rows++)
{
for(columns = 0;columns < 3; columns++)
{
scanf("%d", &m[rows][columns]);
}
}
x = (m[1][1] * m[2][2]) - (m[2][1] * m[1][2]);
y = (m[1][0] * m[2][2]) - (m[2][0] * m[1][2]);
z = (m[1][0] * m[2][1]) - (m[2][0] * m[1][1]);
determinant = (m[0][0] * x) - (m[0][1] * y) + (m[0][2] * z);
printf("\n The Determinant of 3 * 3 Matrix = %d", determinant);
return 0;
}
Output of the above code:
Please Enter the 3 * 3 Matrix Elements
9 7 4
8 6 2
8 6 4
The Determinant of 3 * 3 Matrix = -4
Related Articles
Prime factors of a number in cArmstrong number program in c
Write a program to check leap year in c
C program to find area of rectangle
C program to convert celsius to fahrenheit
Fibonacci series program in C using recursion
Write a program to find area of circle in C
C program to find greatest of three numbers
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative