-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathd18_magic_square.c
More file actions
54 lines (52 loc) · 879 Bytes
/
d18_magic_square.c
File metadata and controls
54 lines (52 loc) · 879 Bytes
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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int magicSquare(int n)
{
int msquare[n][n];
memset(msquare,0,sizeof(msquare));
int i = n/2;
int j = n-1;
for(int k=1;k<=n*n;)
{
if(i==-1 && j==n)
{
j=n-2;
i=0;
}else
{
if(j==n)
j=0;
if(i<0)
i = n -1;
}
if(msquare[i][j])
{
j-=2;
i++;
continue;
}else
{
msquare[i][j] = k++;
}
j++;
i--;
}
printf("Magic Square for n = %d\n",n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",msquare[i][j]);
}
printf("\n");
}
return 0;
}
int main(void)
{
int n;
scanf("%d",&n);
magicSquare(n);
return 0;
}