forked from fineanmol/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicMatrix.c
More file actions
75 lines (68 loc) · 1.79 KB
/
MagicMatrix.c
File metadata and controls
75 lines (68 loc) · 1.79 KB
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
72
73
74
75
//Program to check whether given matrix is magic matrix (has all sum of rows and columns and diagonals are equal) or not
#include <stdio.h>
int main() {
int a[50][50];
int i, j, x, y;
int sum_ref,row_sum,col_sum,diag1_sum,diag2_sum;
// Input for number of rows and columns
printf("\nEnter number of rows and columns : ");
scanf("%d%d", &x, &y);
// Input for first matrix
printf("Enter elements of matrix:\n");
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
printf("Element [%d][%d]: ",i+1, j+1);
scanf("%d", &a[i][j]);
}
}
// Displaying the matrix
printf("\nEntered matrix is:\n");
for (i = 0; i < x; i++) {
for (j = 0; j < y; j++) {
printf("%d\t", a[i][j]);
}
printf("\n");
}
sum_ref=0;
for(j=0;j<y;j++) {
sum_ref=sum_ref+a[0][j];
}
for(i=1;i<x;i++) {
row_sum=0;
for(j=0;j<y;j++) {
row_sum=row_sum+a[i][j];
}
if(row_sum!=sum_ref) {
printf("\nGiven matrix is not magic matrix.");
return 0;
}
}
for(j=0;j<y;j++) {
col_sum=0;
for(i=0;i<x;i++) {
col_sum=col_sum+a[i][j];
}
if(col_sum!=sum_ref) {
printf("\nGiven matrix is not magic matrix.");
return 0;
}
}
diag1_sum=0;
for(i=0;i<x;i++) {
diag1_sum=diag1_sum+a[i][i];
}
if(diag1_sum!=sum_ref) {
printf("\nGiven matrix is not magic matrix.");
return 0;
}
diag2_sum=0;
for(i=0;i<x;i++) {
diag2_sum=diag2_sum+a[i][x-i-1];
}
if(diag2_sum!=sum_ref) {
printf("\nGiven matrix is not magic matrix.");
return 0;
}
printf("\nGiven matrix is MAGIC MATRIX!!");
return 0;
}