forked from Reshma2005/ZohoEnrich
-
Notifications
You must be signed in to change notification settings - Fork 0
/
q12siamesemagicsquare.c
80 lines (65 loc) · 1022 Bytes
/
q12siamesemagicsquare.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
72
73
74
75
76
77
78
79
/*Write a program to construct a magic square of order "N" using Siamese Method */
#include <stdio.h>
void check(int *p1, int *p2,int n)
{
if(*p1==-1)
{
*p1=n-1;
}
if(*p1==n)
{
*p1=0;
}
if(*p1==n+1)
{
*p1=1;
}
if(*p2==-1)
{
*p2=n-1;
}
if(*p2==n)
{
*p2=0;
}
}
int main()
{
int i,j,x,num;
int a[10][10];
printf("Enter the size(odd number) of the magic square:\n");
scanf("%d",&num);
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
a[i][j]=0;
}
}
i=0;
j=(num/2);
a[i][j]=1;
for(x=2;x<=(num*num);x++)
{
i=i-1;
j=j+1;
check(&i,&j,num);
if(a[i][j]!=0)
{
i=i+2;
j=j-1;
}
check(&i,&j,num);
a[i][j]=x;
}
printf("The magic square is:\n");
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
return 0;
}