-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathPrint_Pattern.c
141 lines (119 loc) · 2.52 KB
/
Print_Pattern.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Print a pattern of numbers from 1 to n as shown below. Each of the numbers is separated by a single space.
4 4 4 4 4 4 4
4 3 3 3 3 3 4
4 3 2 2 2 3 4
4 3 2 1 2 3 4
4 3 2 2 2 3 4
4 3 3 3 3 3 4
4 4 4 4 4 4 4
Input Format
The input will contain a single integer n.
Constraints
1 <= n <= 1000
Sample Input 0
2
Sample Output 0
2 2 2
2 1 2
2 2 2
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
Sample Input 2
7
Sample Output 2
7 7 7 7 7 7 7 7 7 7 7 7 7
7 6 6 6 6 6 6 6 6 6 6 6 7
7 6 5 5 5 5 5 5 5 5 5 6 7
7 6 5 4 4 4 4 4 4 4 5 6 7
7 6 5 4 3 3 3 3 3 4 5 6 7
7 6 5 4 3 2 2 2 3 4 5 6 7
7 6 5 4 3 2 1 2 3 4 5 6 7
7 6 5 4 3 2 2 2 3 4 5 6 7
7 6 5 4 3 3 3 3 3 4 5 6 7
7 6 5 4 4 4 4 4 4 4 5 6 7
7 6 5 5 5 5 5 5 5 5 5 6 7
7 6 6 6 6 6 6 6 6 6 6 6 7
7 7 7 7 7 7 7 7 7 7 7 7 7
*/
#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 base = 1 + (2*(n-1));
int initial, last;
initial = 0;
last = base;
int i, x = n;
while(x >= 1)
{
i = n;
while(i != x)
{
printf("%d ", i);
i--;
}
for(i = initial; i < last; i++)
{
printf("%d ", x);
}
i = x;
while(i != n)
{
printf("%d ", i+1);
i++;
}
printf("\n");
initial += 1;
last -= 1;
x -= 1;
}
initial = last = base/2;
x = 2;
int count = 0;
while(x <= n)
{
i = 0;
int j;
j = n;
while(i != initial)
{
printf("%d ", j);
i++;
j--;
}
for(i = 0; i <= count; i++)
{
printf("%d ", x);
}
i = last;
j = x;
while(i != 0)
{
printf("%d ", j);
j++;
i--;
}
count += 2;
initial -= 1;
last -= 1;
x += 1;
printf("\n");
}
return 0;
}