-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmagic.c
More file actions
70 lines (70 loc) · 1.32 KB
/
magic.c
File metadata and controls
70 lines (70 loc) · 1.32 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
#include<stdio.h>
#include<stdlib.h>
int main()
{
int order,i,j;
int **array;
int up=0,right,square,number;
printf("Enter the order of the matrix:");
scanf("%d",&order);
array=malloc(order*sizeof(int *));
for(i=0;i<order;i++)
{
array[i]=malloc(order*sizeof(int *));
}
right=order/2;
square=order*order;
if(order%2!=0)
{
for (number=1;number<=square;)
{
if (up==-1 && right==order)
{
right=order-1;
up=1;
}
else
{
if (right==order)
{
right=0;
}
if (up==-1)
{
up=order-1;
}
}
if (array[up][right])
{
right=right-1;
up=up+2;
continue;
}
else
{
array[up][right]=number++;
}
right++;
up--;
}
printf("\n");
for(i=0;i<order;i++)
{
for(j=0;j<order;j++)
{
printf("%d\t ",array[i][j]);
}
printf("\n");
}
}
else
{
printf("No magic square is possible in this method");
}
for(i=0;i<order;i++)
{
free(array[i]);
}
free(array);
return 0;
}