-
Notifications
You must be signed in to change notification settings - Fork 0
/
magicsuare.c
68 lines (65 loc) · 1.94 KB
/
magicsuare.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
import java.util.*;
class Test{
//driver code
public static void main (String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int i,j,sum=0,sm=0,f=0;
int a[][]=new int[n][n];
for(i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j]=sc.nextInt();
for(i=0;i<n;i++){
for(j=0;j<n;j++){
System.out.print(a[i][j]+" ");
}
System.out.println();
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i==j){
sum=sum+a[i][j];
// System.out.println(a[i][j]);
}
}
}
// System.out.println(sum);
for(i=0;i<n;i++)
{ sm=0;
for(j=0;j<n;j++)
{
sm=sm+a[i][j];
}
if(sm!=sum){
f=1;
break;
}
}
// System.out.println(sm);
for(i=0;i<n;i++)
{ sm=0;
for(j=0;j<n;j++)
{
sm=sm+a[j][i];
}
if(sm!=sum){
f=1;
break;
}
}
// System.out.println(sm);
sm=0;
for(i=n-1,j=0;(i>=0&j<n);i--,j++)
{
sm=sm+a[i][j];
}
if(sm!=sum)
f=1;
// System.out.println(sm);
if(f==0)
System.out.println("Magic Square");
else
System.out.println("Not a Magic Square");
}
}