-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp19.c
57 lines (47 loc) · 1003 Bytes
/
p19.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
/**
* C program to print perfect square number pattern
*/
#include <stdio.h>
#define SIZE 10 // Size is always even
int main()
{
int i, j, N;
int board[SIZE][SIZE];
int left, top;
left = 0;
top = SIZE - 1;
N = 1;
for(i=1; i<=SIZE/2; i++, left++, top--)
{
// Fill from left to right
for(j=left; j<=top; j++, N++)
{
board[left][j] = N;
}
// Fill from top to down
for(j=left+1; j<=top; j++, N++)
{
board[j][top] = N;
}
// Fill from right to left
for(j=top-1; j>=left; j--, N++)
{
board[top][j] = N;
}
// Fill from down to top
for(j=top-1; j>=left+1; j--, N++)
{
board[j][left] = N;
}
}
// Print the pattern
for(i=0; i<SIZE; i++)
{
for(j=0; j<SIZE; j++)
{
printf("%-5d", board[i][j]);
}
printf("\n");
}
return 0;
}