-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path38_pattern.c
160 lines (121 loc) · 4.05 KB
/
38_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// // C proram to print following Pattern
/*
Pattern 38.
1
212
32123
4321234
32123
212
1
*/
// // Header Files
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
// // Main Function Start
int main()
{
int maxRows, maxCols;
printf("\nHow Many Rows => ");
scanf("%d", &maxRows);
// // Handling Invalid Input
if (maxRows < 1)
{
printf("\n!!! Invalid Input,Plz Enter Positive Number....");
exit(0);
}
// // Determine Number of Columns According to maxRows
maxCols = maxRows;
// // Print Pattern
puts("\n--------------------------------------------\n");
// // 1st Approach
int colsInCurrentRow = -1, spacesInCurrentRow, halfOfCols = (maxCols + 1) / 2, numAtCol = 0;
for (int row = 1; row <= maxRows; row++)
{
if (row <= halfOfCols)
{
colsInCurrentRow += 2;
numAtCol = row;
}
else
{
colsInCurrentRow -= 2;
numAtCol = maxRows + 1 - row;
}
// // If user wants even number of rows
if (row == halfOfCols + 1 && maxRows % 2 == 0)
colsInCurrentRow += 2;
spacesInCurrentRow = maxCols - colsInCurrentRow;
for (int space = 1; space <= spacesInCurrentRow / 2; space++)
printf(" ");
for (int col = 1; col <= colsInCurrentRow; col++)
{
printf("%2d ", col < (colsInCurrentRow + 1) / 2 ? numAtCol-- : numAtCol++);
}
printf("\n");
}
// // 2nd Approach
// // int spacesInCurrentRow, halfOfCols = (maxCols + 1) / 2;
// // for (int row = 1; row <= maxRows; row++)
// // {
// // spacesInCurrentRow = row <= halfOfCols ? halfOfCols - row : row - halfOfCols;
// // // // If user wants even number of rows
// // if (row >= halfOfCols + 1 && maxRows % 2 == 0)
// // spacesInCurrentRow--;
// // for (int space = 1; space <= spacesInCurrentRow; space++)
// // printf(" ");
// // for (int col = row <= halfOfCols ? row : maxRows + 1 - row; col; col--)
// // printf("%2d ", col);
// // for (int col = 2; col <= (row <= halfOfCols ? row : maxRows + 1 - row); col++)
// // printf("%2d ", col);
// // printf("\n");
// // }
// // 3rd Approach
// // int spacesInCurrentRow, halfOfCols = (maxCols + 1) / 2, numAtCol = 0;
// // for (int row = 1; row <= maxRows; row++)
// // {
// // if (row <= halfOfCols)
// // {
// // spacesInCurrentRow = halfOfCols - row;
// // numAtCol = row;
// // }
// // else
// // {
// // spacesInCurrentRow = row - halfOfCols;
// // numAtCol = maxRows + 1 - row;
// // }
// // // // If user wants even number of rows
// // if (row >= halfOfCols + 1 && maxRows % 2 == 0)
// // spacesInCurrentRow--;
// // for (int space = 1; space <= spacesInCurrentRow; space++)
// // printf(" ");
// // for (int col = numAtCol; col; col--)
// // printf("%2d ", col);
// // for (int col = 2; col <= numAtCol; col++)
// // printf("%2d ", col);
// // printf("\n");
// // }
// // 4th Approach
// // int halfOfCols = (maxCols + 1) / 2, vary = 1, numAtCol;
// // for (int row = 1; row <= maxRows; row++)
// // {
// // numAtCol = row <= halfOfCols ? row : maxRows + 1 - row;
// // for (int col = 1; col <= maxCols; col++)
// // {
// // if (col >= halfOfCols + 1 - vary && col <= halfOfCols - 1 + vary)
// // printf("%2d ", col < halfOfCols ? numAtCol-- : numAtCol++);
// // else
// // printf(" ");
// // }
// // row < halfOfCols ? vary++ : vary--;
// // // // If user wants even number of rows
// // if (row == halfOfCols && maxRows % 2 == 0)
// // vary++;
// // printf("\n");
// // }
printf("\n");
getch();
return 0;
}
// // Main Function End