-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicSquare.java
More file actions
57 lines (57 loc) · 1.06 KB
/
MagicSquare.java
File metadata and controls
57 lines (57 loc) · 1.06 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
//A magic square is a square matrix of distinct numbers in which the sum of the numbers in each row, column and diagonal is equal. The sum is called the magic sum.
//Input:
//4 3 8
//9 5 1
//2 7 6
//Output: 15
import java.util.*;
public class MagicSquare
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int a[][]=new int[5][5];
System.out.println("Enter a 3x3 Matrix");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
a[i][j]=in.nextInt();
}
}
int i=0,j=0,row1=0,row2=0,row3=0,col1=0,col2=0,col3=0;
for(j=0;j<3;j++)
{
row1+=a[i][j];
}i++;
for(j=0;j<3;j++)
{
row2+=a[i][j];
}i++;
for(j=0;j<3;j++)
{
row3+=a[i][j];
}
j=0;
for(i=0;i<3;i++)
{
col1+=a[i][j];
}j++;
for(i=0;i<3;i++)
{
col2+=a[i][j];
}j++;
for(i=0;i<3;i++)
{
col3+=a[i][j];
}
if(row1==row2&&row2==row3&&col1==col2&&col2==col3)
{
System.out.println("Its's a Magic Square!");
}
else
{
System.out.println("Its's not a Magic Square!");
}
}
}