Determinant of a Matrix
In this post, you will learn how to find determinant of a 2x2 matrix and 3x3 matrix in 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
Python Program to find the determinant of a Matrix 2x2
In the given program, we are calculating the determinant of a 2X2 numpy matrix using the numpy.linalg.det() function.
# importing Numpy package
import numpy as np
# Creating a 2X2 numpy matrix
arr = np.array([[30, 12], [20, 11]])
# Displaying the Matrix
print("Numpy Matrix =")
print(arr)
# Calculating the determinant of matrix
determinant = np.linalg.det(arr)
print("\nThe Determinant of 2 * 2 Matrix =")
print(int(determinant))
Output of the above code:
Numpy Matrix =
[[30 12]
[20 11]]
The Determinant of 2 * 2 Matrix =
90
Java 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.
import java.util.Scanner;
public class DeterminantOfMatrix {
private static Scanner sc;
public static void main(String[] args) {
int[][] arr = new int[2][2];
int rows, cols, determinant = 0;
sc= new Scanner(System.in);
System.out.println("\n Please Enter the Matrix Items : ");
for(rows = 0; rows < 2; rows++) {
for(cols = 0; cols < 2; cols++) {
arr[rows][cols] = sc.nextInt();
}
}
determinant = (arr[0][0] * arr[1][1]) - (arr[0][1] * arr[1][0]);
System.out.println("The Determinant of 2 * 2 Matrix = " + determinant );
}
}
Output of the above code:
Please Enter the Matrix Items :
6 3
6 21
The Determinant of 2 * 2 Matrix = 108
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