forked from AkshayNachappa/Hacktoberfest_101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Determinant.c
71 lines (68 loc) · 1.48 KB
/
Determinant.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
main()
{
int A[100][100];
int i,j,k,n,res;
printf("Enter the order of the matrix: \n");
scanf("%d",&n);
printf("\nEnter the elements of the matrix one by one: \n");
for(i = 0 ; i < n ; i++)
{
for(j = 0 ; j < n ; j++)
{
scanf("%d",&A[i][j]);
}
}
for(i = 0 ; i < n ; i++)
{
for(j = 0 ; j < n ; j++)
{
printf("%5d",A[i][j]);
}
printf("\n");
}
res = det(A,n);
printf("%d",res);
}
int det(int A[100][100], int n)
{
int Minor[100][100];
int i,j,k,c1,c2;
int determinant;
int c[100];
int O=1;
if(n == 2)
{
determinant = 0;
determinant = A[0][0]*A[1][1]-A[0][1]*A[1][0];
return determinant;
}
else
{
for(i = 0 ; i < n ; i++)
{
c1 = 0, c2 = 0;
for(j = 0 ; j < n ; j++)
{
for(k = 0 ; k < n ; k++)
{
if(j != 0 && k != i)
{
Minor[c1][c2] = A[j][k];
c2++;
if(c2>n-2)
{
c1++;
c2=0;
}
}
}
}
determinant = determinant + O*(A[0][i]*det(Minor,n-1));
O=-1*O;
}
}
return determinant;
}