-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrinting Pattern Using Loops.c
82 lines (74 loc) · 1.41 KB
/
Printing Pattern Using Loops.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
80
81
82
/*
Sample Input 1
5
Sample Output 1
5 5 5 5 5 5 5 5 5
5 4 4 4 4 4 4 4 5
5 4 3 3 3 3 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 2 1 2 3 4 5
5 4 3 2 2 2 3 4 5
5 4 3 3 3 3 3 4 5
5 4 4 4 4 4 4 4 5
5 5 5 5 5 5 5 5 5
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
// Complete the code to print the pattern.
int line, col, col_limit;
int num;
for(line=1; line<n; line++)
{
num=n;
for(col=1;col<=line;col++)
{
printf("%d ",num);
--num;
}
int diff=n-line+1;
for(col=1;col<=2*(n-line)-1;col++)
printf("%d ",diff);
for(col=1;col<=line;col++)
{
++num;
printf("%d ",num);
}
printf("\n");
}
for(col=1;col<=2*n-1;col++)
{
if(col<=n)
printf("%d ",n-col+1);
else {
printf("%d ",col-n+1);
}
}
printf("\n");
for(line=n+1;line<=2*n-1;line++)
{
num=n;
for(col=line;col<=2*n-1;col++)
{
printf("%d ",num);
num--;
}
int diff=line-n+1;
for(col=1;col<=2*(line-n)-1;col++)
{
printf("%d ",diff);
}
for(col=line;col<=2*n-1;col++)
{
++num;
printf("%d ",num);
}
printf("\n");
}
return 0;
}